By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,271 Members | 1,722 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,271 IT Pros & Developers. It's quick & easy.

regulars expressions ?

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
re.findall(r'"?(.+?)"?(?:,|$)', yourtexthere)

Jul 19 '05 #4

P: n/a
Oh, oops, sorry, that code doesn't respect the quotes.
Use this code:
re.findall(r'(".+?"|\S+)(?:,|$)', yourtexthere)

Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.