473,406 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Split a string representing a list of lists.


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:ad@ionicsoft.com
http://www.ionicsoft.com
Jul 26 '06 #1
6 1501
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[i]);
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[i]='('+arr[i]+')';
for (i=0;i<arr.length;i++) alert(arr[i]);

Hope this helps,

--
Bart

Jul 26 '06 #2

Hi Bart,

"Bart Van der Donck" <ba**@nijlen.comwrites:
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[i]);
>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[i]='('+arr[i]+')';
for (i=0;i<arr.length;i++) alert(arr[i]);

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:ad@ionicsoft.com
http://www.ionicsoft.com
Jul 26 '06 #3
Bart Van der Donck wrote:
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[i]);
>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[i]='('+arr[i]+')';
for (i=0;i<arr.length;i++) alert(arr[i]);

Hope this helps,
Or even just:

/\((.*)\)/.exec(str)[1].split(/,\s*/)

which works for both.
Jul 26 '06 #4
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.
Jul 26 '06 #5

Hi Duncan,

Duncan Booth <du**********@invalid.invalidwrites:
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:ad@ionicsoft.com
http://www.ionicsoft.com
Jul 26 '06 #6

Arnaud Diederen aundro wrote:
Hi Duncan,

Duncan Booth <du**********@invalid.invalidwrites:
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 !

Jul 27 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
5
by: oliver | last post by:
hi there i'm experimanting with imaplib and came across stringts like (\HasNoChildren) "." "INBOX.Sent Items" in which the quotes are part of the string. now i try to convert this into a...
2
by: ThurstonHowl | last post by:
Hello, my task is the following: Input are tables with fields containing strings where the strings are actually delimited lists. For example, one field could contain 'AB|CD|EF|GH' I've...
8
by: Wijaya Edward | last post by:
Hi all, I was trying to split a string that represent chinese characters below: ??? But why the split function here doesn't seem
1
by: maxtoroq | last post by:
I know how to work the .FindAll method of the List class, but is there a way to split a list into 2 lists, one containing all the items that match a certain criteria and one that doesn't?
9
by: jm.suresh | last post by:
Hi, I have a string '((1,2), (3,4))' and I want to convert this into a python tuple of numbers. But I do not want to use eval() because I do not want to execute any code in that string and limit...
1
by: kellysgirl | last post by:
Now what you are going to see posted here is both the set of instructions I was given..and the code I have written. The instructions I was given are as follows In this case, you will create...
2
by: Shawn Minisall | last post by:
I'm trying to unpack a list of 5 floats from a list read from a file and python is telling me 5 variables are too many for the string.split statement. Anyone have any other idea's? NOTE: the only...
5
by: Robert Dodier | last post by:
Hello, I'd like to split a string by commas, but only at the "top level" so to speak. An element can be a comma-less substring, or a quoted string, or a substring which looks like a function...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.