Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

regulars expressions ?

2 views
Skip to first unread message

scott

unread,
Jun 28, 2005, 1:45:31 PM6/28/05
to
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

Jaime Wyant

unread,
Jun 28, 2005, 1:57:52 PM6/28/05
to scott, pytho...@python.org
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

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

Jaime Wyant

unread,
Jun 28, 2005, 1:58:39 PM6/28/05
to scott, pytho...@python.org
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 <progra...@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:

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

Devan L

unread,
Jun 28, 2005, 3:13:36 PM6/28/05
to
re.findall(r'"?(.+?)"?(?:,|$)', yourtexthere)

Devan L

unread,
Jun 28, 2005, 3:20:45 PM6/28/05
to
Oh, oops, sorry, that code doesn't respect the quotes.
Use this code:
re.findall(r'(".+?"|\S+)(?:,|$)', yourtexthere)

0 new messages