regulars expressions ?

Discussion in 'Python' started by scott, Jun 28, 2005.

  1. scott

    scott Guest

    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
    scott, Jun 28, 2005
    #1
    1. Advertising

  2. scott

    Jaime Wyant Guest

    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 <> 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
    >
    Jaime Wyant, Jun 28, 2005
    #2
    1. Advertising

  3. scott

    Jaime Wyant Guest

    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 <> 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 <> 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
    > >

    >
    Jaime Wyant, Jun 28, 2005
    #3
  4. scott

    Devan L Guest

    re.findall(r'"?(.+?)"?(?:,|$)', yourtexthere)
    Devan L, Jun 28, 2005
    #4
  5. scott

    Devan L Guest

    Oh, oops, sorry, that code doesn't respect the quotes.
    Use this code:
    re.findall(r'(".+?"|\S+)(?:,|$)', yourtexthere)
    Devan L, Jun 28, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jay Douglas
    Replies:
    0
    Views:
    597
    Jay Douglas
    Aug 15, 2003
  2. Chris Hills
    Replies:
    18
    Views:
    480
    Richard Bos
    Sep 6, 2006
  3. jacob navia

    "regulars" do not use debuggers

    jacob navia, Jul 31, 2009, in forum: C Programming
    Replies:
    26
    Views:
    785
  4. santosh

    What happened to some regulars?

    santosh, Aug 20, 2011, in forum: C Programming
    Replies:
    6
    Views:
    510
    Charles Richmond
    Aug 20, 2011
  5. Question for the regulars

    , Oct 16, 2005, in forum: Perl Misc
    Replies:
    66
    Views:
    643
    John W. Kennedy
    Nov 3, 2005
Loading...

Share This Page