473,499 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

regulars expressions ?

hi people !

<newbie>
i got some trouble with regular expressions
i need to split a string like this on the ',' character :

mystring = ""\test1, test2\", test, 42"

i wanna get something (a list) like this (3 elements) :
"test1, test2"
test
42

but the only thing i get is a list like this (4 elements) :
"test1"
"test2"
test
42

each element is separated by ',' but 1st element which is delimited by
'"' may contain ',' character inside.

so the regular expression i need is something like :
split each element using ',' delimiter but if ',' delimiter is included
between '"' please do not split
</newbie>

1st question is : does someone has understood the question ?
2nd question is : does someone has an answer ?

thanks people

scott
Jul 19 '05 #1
4 1069
Maybe, you need the csv module:

import csv
mystring = "\"test1, test2\", test, 42"

# The one argument to csv.reader is an iterable object
# You could use a file here...
csv_reader = csv.reader([mystring])

for line in csv_reader:
print line

['test1, test2', ' test', ' 42']

hth,
jw

On 6/28/05, scott <sc***@alussinan.org> wrote:
hi people !

<newbie>
i got some trouble with regular expressions
i need to split a string like this on the ',' character :

mystring = ""\test1, test2\", test, 42"

i wanna get something (a list) like this (3 elements) :
"test1, test2"
test
42

but the only thing i get is a list like this (4 elements) :
"test1"
"test2"
test
42

each element is separated by ',' but 1st element which is delimited by
'"' may contain ',' character inside.

so the regular expression i need is something like :
split each element using ',' delimiter but if ',' delimiter is included
between '"' please do not split
</newbie>

1st question is : does someone has understood the question ?
2nd question is : does someone has an answer ?

thanks people

scott
--
http://mail.python.org/mailman/listinfo/python-list

Jul 19 '05 #2
Doh - please note that csv.reader takes more than one argument - the
FIRST one is an iterable object.

jw

On 6/28/05, Jaime Wyant <pr***********@gmail.com> wrote:
Maybe, you need the csv module:

import csv
mystring = "\"test1, test2\", test, 42"

# The one argument to csv.reader is an iterable object
# You could use a file here...
csv_reader = csv.reader([mystring])

for line in csv_reader:
print line

['test1, test2', ' test', ' 42']

hth,
jw

On 6/28/05, scott <sc***@alussinan.org> wrote:
hi people !

<newbie>
i got some trouble with regular expressions
i need to split a string like this on the ',' character :

mystring = ""\test1, test2\", test, 42"

i wanna get something (a list) like this (3 elements) :
"test1, test2"
test
42

but the only thing i get is a list like this (4 elements) :
"test1"
"test2"
test
42

each element is separated by ',' but 1st element which is delimited by
'"' may contain ',' character inside.

so the regular expression i need is something like :
split each element using ',' delimiter but if ',' delimiter is included
between '"' please do not split
</newbie>

1st question is : does someone has understood the question ?
2nd question is : does someone has an answer ?

thanks people

scott
--
http://mail.python.org/mailman/listinfo/python-list

Jul 19 '05 #3
re.findall(r'"?(.+?)"?(?:,|$)', yourtexthere)

Jul 19 '05 #4
Oh, oops, sorry, that code doesn't respect the quotes.
Use this code:
re.findall(r'(".+?"|\S+)(?:,|$)', yourtexthere)

Jul 19 '05 #5

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

Similar topics

72
4329
by: Raymond Hettinger | last post by:
Peter Norvig's creative thinking triggered renewed interest in PEP 289. That led to a number of contributors helping to re-work the pep details into a form that has been well received on the...
5
1896
by: jens | last post by:
I was wondering if anyone could tell be whether it's possible to instruct preg_match not to save subexpressions matches under 0, 1...n keys when using named subexpressions; I.e instruct preg_named...
2
4978
by: Sehboo | last post by:
Hi, I have several regular expressions that I need to run against documents. Is it possible to combine several expressions in one expression in Regex object. So that it is faster, or will I...
4
5137
by: Együd Csaba | last post by:
Hi All, I'd like to "compress" the following two filter expressions into one - assuming that it makes sense regarding query execution performance. .... where (adate LIKE "2004.01.10 __:30" or...
2
1992
by: aarklon | last post by:
Hi all, Recently i was reading a c book which contained a section called C pitfalls. it had a paragraph on the following lines:- ...
3
1740
by: R. Rajesh Jeba Anbiah | last post by:
I just checked the new GG Beta and came across this interesting ratings (ratings given by regulars/newbies to c.l.php regulars). I guess this might surprise for some. Poster - Avg -...
18
1375
by: strangerdream | last post by:
I'm a lurker here, and I do not find any female regulars here in c.l.c. Is it something thats got to do with their genes?
1
4359
by: Allan Ebdrup | last post by:
I have a dynamic list of regular expressions, the expressions don't change very often but they can change. And I have a single string that I want to match the regular expressions against and find...
35
1654
by: lyle fairfield | last post by:
My observation is that over the past few months many of our regulars seems to be drifting away, making fewer contributions or none at all. Why? I can speculate: 1. The quality of questions...
0
7131
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
7007
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
7220
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...
1
6894
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
7388
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
4600
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...
0
3099
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...
0
3091
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.