Split a string representing a list of lists.

Discussion in 'Javascript' started by Arnaud Diederen, Jul 26, 2006.

  1. Hello everyone,

    I had a look in the archives about this problem but maybe I'm not
    using the proper terms in order to define my problem, and so I
    couldn't find a solution.

    I was just wondering if there was a regex that would help me split a
    string of the form

    "(0, 1, 2)"
    into an array of 3 elements: ["0", "1", "2"]

    and also split

    "((0, 1, 2), (3, 4, 5), (6, 7, 8))"
    into an array of 3 elements: ["(0, 1, 2)", "(3, 4, 5)", "(6, 7, 8)"]

    I'm not an expert in regexes at all, and I couldn't find it.
    Do I have to iterate on the input string and do the split myself, or
    can someone provide with a solution?

    Thank you very much,

    Arnaud


    PS: I just thought about that: replacing the '(' and ')' with '[' and
    ']' respectively, and then eval()'ing the thing to get n-deep
    arrays.. but eval is evil, so I'd prefer not using it if possible.


    --
    Arnaud DIEDEREN
    Software Developer
    IONIC Software
    Rue de Wallonie, 18 - 4460 Grace-Hollogne - Belgium
    Tel: +32.4.3640364 - Fax: +32.4.2534737
    mailto:
    http://www.ionicsoft.com
     
    Arnaud Diederen, Jul 26, 2006
    #1
    1. Advertising

  2. Arnaud Diederen wrote:

    > I was just wondering if there was a regex that would help me split a
    > string of the form "(0, 1, 2)" into an array of 3 elements: ["0", "1", "2"]


    var str = '(0, 1, 2)';
    var arr = str.replace(/(^\(|\s|\)$)/g,'').split(',');
    for (i=0;i<arr.length;i++) alert(arr);

    > and also split
    > "((0, 1, 2), (3, 4, 5), (6, 7, 8))"
    > into an array of 3 elements: ["(0, 1, 2)", "(3, 4, 5)", "(6, 7, 8)"]


    var str = '((0, 1, 2), (3, 4, 5), (6, 7, 8))';
    var arr = str.replace(/(\(|\){2}$|\t)/g,'').split('), ');
    for (i=0;i<arr.length;i++) arr='('+arr+')';
    for (i=0;i<arr.length;i++) alert(arr);

    Hope this helps,

    --
    Bart
     
    Bart Van der Donck, Jul 26, 2006
    #2
    1. Advertising

  3. Hi Bart,

    "Bart Van der Donck" <> writes:

    > Arnaud Diederen wrote:
    >
    >> I was just wondering if there was a regex that would help me split a
    >> string of the form "(0, 1, 2)" into an array of 3 elements: ["0", "1", "2"]

    >
    > var str = '(0, 1, 2)';
    > var arr = str.replace(/(^\(|\s|\)$)/g,'').split(',');
    > for (i=0;i<arr.length;i++) alert(arr);
    >
    >> and also split
    >> "((0, 1, 2), (3, 4, 5), (6, 7, 8))"
    >> into an array of 3 elements: ["(0, 1, 2)", "(3, 4, 5)", "(6, 7, 8)"]

    >
    > var str = '((0, 1, 2), (3, 4, 5), (6, 7, 8))';
    > var arr = str.replace(/(\(|\){2}$|\t)/g,'').split('), ');
    > for (i=0;i<arr.length;i++) arr='('+arr+')';
    > for (i=0;i<arr.length;i++) alert(arr);
    >
    > Hope this helps,


    It does, really :)

    Thank you very much for your help!

    Regards,
    Arnaud

    >
    > --
    > Bart
    >


    --
    Arnaud DIEDEREN
    Software Developer
    IONIC Software
    Rue de Wallonie, 18 - 4460 Grace-Hollogne - Belgium
    Tel: +32.4.3640364 - Fax: +32.4.2534737
    mailto:
    http://www.ionicsoft.com
     
    Arnaud Diederen, Jul 26, 2006
    #3
  4. Hi Duncan,

    Duncan Booth <> writes:

    > Duncan Booth wrote:
    >
    >> Or even just:
    >>
    >> /\((.*)\)/.exec(str)[1].split(/,\s*/)
    >>
    >> which works for both.

    >
    > Idiot that I am that of course doesn't work. I still think the original
    > proposed solution looks too complex though.


    To be honest, I need to build arrays of numbers, or arrays of arrays
    of numbers (some people would erroneously call these jagged arrays -I
    remember pretty long thread about it- , but I won't get them started
    on that ;) ), or even arrays of arrays of arrays of numbers.
    From those, I would build geometries. So I went the eval() way, even
    though it is bad:

    eval (str.replace (/\(/g, "[") // ( -> [
    .replace (/\)/g, "]") // ) -> ]
    .replace (/(\d)\s+([\d\-])/g, "$1, $2") // ...? ?... -> ...?, ?...
    );

    so, with the string (newlines inserted to add clarity):

    "(
    (
    (
    -82.78750000000001 -1.3425000000000011,
    -92.63250000000001 2.6850000000000023,
    -94.4225 12.977500000000006,
    -90.395 19.242500000000007,
    -79.65499999999999 24.165000000000006,
    -63.09750000000001 20.137500000000003,
    -62.64999999999999 5.3700000000000045,
    -70.2575 -4.4750000000000085,
    -82.78750000000001 -1.3425000000000011
    )
    )
    )"

    which is a WKT (well-known-text) representation of the data of a
    multi-polygon (a geometry that holds 1 or many polygons, each of which
    hold 1 or many rings (portions)), I get the following array:

    [ // Multi Polygon data
    [ // First Polygon data
    [ // First portion/ring of the polygon data
    -82.78750000000001, -1.3425000000000011,
    -92.63250000000001, 2.6850000000000023,
    -94.4225, 12.977500000000006,
    -90.395, 19.242500000000007,
    -79.65499999999999, 24.165000000000006,
    -63.09750000000001, 20.137500000000003,
    -62.64999999999999, 5.3700000000000045,
    -70.2575, -4.4750000000000085,
    -82.78750000000001, -1.3425000000000011
    ]
    ]
    ]

    Which is just what I need in order to build the multi-polygon object.

    I find it to be the best solution so far.

    Best regards,
    Arnaud


    --
    Arnaud DIEDEREN
    Software Developer
    IONIC Software
    Rue de Wallonie, 18 - 4460 Grace-Hollogne - Belgium
    Tel: +32.4.3640364 - Fax: +32.4.2534737
    mailto:
    http://www.ionicsoft.com
     
    Arnaud Diederen, Jul 26, 2006
    #4
  5. Arnaud Diederen

    struggle Guest

    Arnaud Diederen aundro wrote:
    > Hi Duncan,
    >
    > Duncan Booth <> writes:
    >
    > > Duncan Booth wrote:
    > >
    > >> Or even just:
    > >>
    > >> /\((.*)\)/.exec(str)[1].split(/,\s*/)
    > >>
    > >> which works for both.

    > >
    > > Idiot that I am that of course doesn't work. I still think the original
    > > proposed solution looks too complex though.

    >
    > To be honest, I need to build arrays of numbers, or arrays of arrays
    > of numbers (some people would erroneously call these jagged arrays -I
    > remember pretty long thread about it- , but I won't get them started
    > on that ;) ), or even arrays of arrays of arrays of numbers.
    > From those, I would build geometries. So I went the eval() way, even
    > though it is bad:
    >
    > eval (str.replace (/\(/g, "[") // ( -> [
    > .replace (/\)/g, "]") // ) -> ]
    > .replace (/(\d)\s+([\d\-])/g, "$1, $2") // ...? ?... -> ...?, ?...
    > );
    >
    > so, with the string (newlines inserted to add clarity):
    >
    > "(
    > (
    > (
    > -82.78750000000001 -1.3425000000000011,
    > -92.63250000000001 2.6850000000000023,
    > -94.4225 12.977500000000006,
    > -90.395 19.242500000000007,
    > -79.65499999999999 24.165000000000006,
    > -63.09750000000001 20.137500000000003,
    > -62.64999999999999 5.3700000000000045,
    > -70.2575 -4.4750000000000085,
    > -82.78750000000001 -1.3425000000000011
    > )
    > )
    > )"
    >
    > which is a WKT (well-known-text) representation of the data of a
    > multi-polygon (a geometry that holds 1 or many polygons, each of which
    > hold 1 or many rings (portions)), I get the following array:
    >
    > [ // Multi Polygon data
    > [ // First Polygon data
    > [ // First portion/ring of the polygon data
    > -82.78750000000001, -1.3425000000000011,
    > -92.63250000000001, 2.6850000000000023,
    > -94.4225, 12.977500000000006,
    > -90.395, 19.242500000000007,
    > -79.65499999999999, 24.165000000000006,
    > -63.09750000000001, 20.137500000000003,
    > -62.64999999999999, 5.3700000000000045,
    > -70.2575, -4.4750000000000085,
    > -82.78750000000001, -1.3425000000000011
    > ]
    > ]
    > ]
    >
    > Which is just what I need in order to build the multi-polygon object.
    >
    > I find it to be the best solution so far.
    >

    Good solusion , as I am a newbie to javascript , this impress me very
    much !
     
    struggle, Jul 27, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Carlos Ribeiro
    Replies:
    11
    Views:
    708
    Alex Martelli
    Sep 17, 2004
  2. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    413
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  3. Adam
    Replies:
    0
    Views:
    375
  4. Sam Kong
    Replies:
    5
    Views:
    248
    Rick DeNatale
    Aug 12, 2006
  5. Stanley Xu
    Replies:
    2
    Views:
    623
    Stanley Xu
    Mar 23, 2011
Loading...

Share This Page