Split a string representing a list of lists.

A

Arnaud Diederen

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:[email protected]
http://www.ionicsoft.com
 
B

Bart Van der Donck

Arnaud said:
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,
 
A

Arnaud Diederen

Hi Bart,

Bart Van der Donck said:
Arnaud said:
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

--
Arnaud DIEDEREN
Software Developer
IONIC Software
Rue de Wallonie, 18 - 4460 Grace-Hollogne - Belgium
Tel: +32.4.3640364 - Fax: +32.4.2534737
mailto:[email protected]
http://www.ionicsoft.com
 
A

Arnaud Diederen

Hi Duncan,

Duncan Booth said:
Duncan said:
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:[email protected]
http://www.ionicsoft.com
 
S

struggle

Arnaud said:
Hi Duncan,

Duncan Booth said:
Duncan said:
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 !
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top