python regular expression help

Qilong Ren qilong_ren at yahoo.com
Thu Apr 12 03:44:47 EDT 2007


Hi,

Yeah, a little bit tricky. Actually it is part of some Fortran input file.

Thanks for suggestion! It helps a lot!

Thanks,Qilong

----- Original Message ----
From: Gabriel Genellina <gagsl-py2 at yahoo.com.ar>
To: python-list at python.org
Sent: Wednesday, April 11, 2007 9:50:00 PM
Subject: Re: python regular expression help

En Wed, 11 Apr 2007 23:14:01 -0300, Qilong Ren <qilong_ren at yahoo.com>  
escribió:

> Thanks for reply. That actually is not what I want. Strings I am dealing  
> with may look like this:
>      s = 'a = 4.5 b = 'h'  'd' c = 4.5 3.5'
> What I want is
>      a = 4.5
>      b = 'h' 'd'
>      c = 4.5 3.5

That's a bit tricky. You have LHS = RHS where RHS includes all the  
following text *except* the very next word before the following = (which  
is the LHS of the next expression). Or something like that :)

py> import re
py> s = "a = 4.5 b = 'h'  'd' c = 4.5 3.5"
py> r = re.compile(r"\w+\s*=\s*.*?(?=\w+\s*=|$)")
py> for item in r.findall(s):
...   print item
...
a = 4.5
b = 'h'  'd'
c = 4.5 3.5

-- 
Gabriel Genellina

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







       
____________________________________________________________________________________
Don't pick lemons.
See all the new 2007 cars at Yahoo! Autos.
http://autos.yahoo.com/new_cars.html 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070412/db68950c/attachment.html>


More information about the Python-list mailing list