473,789 Members | 2,550 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regular Expressions to Split Lists Into Sub-Lists

For example, given a string "A, B, C (P, Q, R), D (X, Y [K, L, M ,N],
Z)".

Would like to split into tokens thusly:

a[0] == "A"
a[1] == "B"
a[2] == "C (P, Q, R)"
a[3] == "D (X, Y [K, L, M ,N], Z)"

i.e. do not descend into sub-lists

PHP split() using commas as a delimiter will give 14 tokens.

I can write a routine which checks the input byte by byte and
increments or decrements a counter based on how many opening "( [ {"
or closing ") ] }" brackets it sees. If counter 0, this means ignore
delimiters (i.e. keep looking). Guaranteed to work, but to my mind
seems to be rather clunky.

Is it possible to extract the tokens using regular expressions? E.g.
substitute highest level commas with a special delimiter say "~", and
split using that delimiter.

Thanks for reading.

Regards,

YR
Aug 17 '08 #1
2 1541
Yimin Rong wrote:
For example, given a string "A, B, C (P, Q, R), D (X, Y [K, L, M ,N],
Z)".

Would like to split into tokens thusly:

a[0] == "A"
a[1] == "B"
a[2] == "C (P, Q, R)"
a[3] == "D (X, Y [K, L, M ,N], Z)"

i.e. do not descend into sub-lists

PHP split() using commas as a delimiter will give 14 tokens.

I can write a routine which checks the input byte by byte and
increments or decrements a counter based on how many opening "( [ {"
or closing ") ] }" brackets it sees. If counter 0, this means ignore
delimiters (i.e. keep looking). Guaranteed to work, but to my mind
seems to be rather clunky.

Is it possible to extract the tokens using regular expressions? E.g.
substitute highest level commas with a special delimiter say "~", and
split using that delimiter.
Seems like you already have your answer here. If the delimiters for
the top-level are different, it shouldn't be a problem to split on them.

--
Curtis
Aug 17 '08 #2
On Aug 17, 6:29 pm, Curtis <dye...@gmail.c omwrote:
Yimin Rong wrote:
For example, given a string "A, B, C (P, Q, R), D (X, Y [K, L, M ,N],
Z)".
Would like tosplitinto tokens thusly:
a[0] == "A"
a[1] == "B"
a[2] == "C (P, Q, R)"
a[3] == "D (X, Y [K, L, M ,N], Z)"
i.e. do not descend into sub-lists
PHPsplit() using commas as a delimiter will give 14 tokens.
I can write a routine which checks the input byte by byte and
increments or decrements a counter based on how many opening "( [ {"
or closing ") ] }" brackets it sees. If counter 0, this means ignore
delimiters (i.e. keep looking). Guaranteed to work, but to my mind
seems to be rather clunky.
Is it possible to extract the tokens using regular expressions? E.g.
substitute highest level commas with a special delimiter say "~", and
splitusing that delimiter.

Seems like you already have your answer here. If the delimiters for
the top-level are different, it shouldn't be a problem tospliton them.

--
Curtis
Agreed, however the step I need is to replace top level delimiters in
the input. Do you think regular expression substitution can do this? /
YR
Aug 18 '08 #3

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

Similar topics

8
2433
by: Michael McGarry | last post by:
Hi, I am horrible with Regular Expressions, can anyone recommend a book on it? Also I am trying to parse the following string to extract the number after load average. ".... load average: 0.04, 0.02, 0.01" how can I extract this number with RE or otherwise?
11
3921
by: Martin Robins | last post by:
I am trying to parse a string that is similar in form to an OLEDB connection string using regular expressions; in principle it is working, but certain character combinations in the string being parsed can completely wreck it. The string I am trying to parse is as follows: commandText=insert into (Text) values (@message + N': ' + @category);commandType=StoredProcedure; message=@message; category=@category I am looking to retrive name value...
7
2223
by: Chris | last post by:
hello, I have question about the re.I option for Regular Expressions: >>> import re >>> re.findall('x', '1x2X3', re.I) as expected finds both lower and uppercase x
6
2377
by: sheffdog | last post by:
Hello, I often find myself cleaning up strings like the following: setAttr ".ftn" -type "string" /assets/chars/ /boya/geo/textures/lod1/ppbhat.tga"; Using regular expressions, the best I can do so far is using the re.sub command but it still takes two lines. Can I do this in one line? Or should I be approaching this differently? All I want to end up with is
9
470
by: www.douglassdavis.com | last post by:
I am using the preg_match function (in PHP) that uses perl regular expressions. Apparently I don't really understand regular expressions though. Could some one explain this? If this is the regular expression /^\s*(\d+\.\d+)|(\.\d+)|(\d+)\s*$/ How does this:
9
1792
by: Schorschi | last post by:
Not having used regular expressions much, I need some help. Given a string... "This\0Guy\0Needs\0Some\0Help\0\0\0\0\0" Need result as array of strings... "This","Guy", "Needs", "Some", "Help" Where '\0' is a literal zero-byte. I think I need two regular expressions? One to strip the multiple instances of '\0' bytes, and another to split the string.
7
3831
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I want to avoid that. My question here is if there is a way to pass either a memory stream or array of "find", "replace" expressions or any other way to avoid multiple copies of a string. Any help will be highly appreciated
25
5170
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART (CONDUCTION DEFECT) 37.33/2 HEART (CONDUCTION DEFECT) WITH CATHETER 37.34/2 " the expression is "HEART (CONDUCTION DEFECT)". How do I gain access to the expression (not the matches) at runtime? Thanks, Mike
10
1691
by: Julien | last post by:
Hi, I'm fairly new in Python and I haven't used the regular expressions enough to be able to achieve what I want. I'd like to select terms in a string, so I can then do a search in my database. query = ' " some words" with and "without quotes " ' p = re.compile(magic_regular_expression) $ <--- the magic happens m = p.match(query)
6
2864
by: chrispoliquin | last post by:
I am using regular expressions to search a string (always full sentences, maybe more than one sentence) for common abbreviations and remove the periods. I need to break the string into different sentences but split('.') doesn't solve the whole problem because of possible periods in the middle of a sentence. So I have... ----------------
0
9511
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10136
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9979
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7525
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6765
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5415
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4090
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2906
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.