python regular expression help

Qilong Ren qilong_ren at yahoo.com
Thu Apr 12 01:09:15 EDT 2007


Hi,

I don't quite understand the regular expression:
  re.compile("[a-z]+.*?(?=[a-z]|$)" )
and I tried. In some cases it works. But if the string looks like:
   s = 'a = 3.4 b = 4.5 5.6 c = "h","d"'
it failed.

What I came up with is :
         names = re.compile(r'(\w+)\s*=').findall(s)
the corresponding values
        values = re.split(r'\w+\s*=',s)[1:]
It dose not look good but it works. What do you think?

Thanks,Qilong



----- Original Message ----
From: 7stud <bbxx789_05ss at yahoo.com>
To: python-list at python.org
Sent: Wednesday, April 11, 2007 8:27:57 PM
Subject: Re: python regular expression help

On Apr 11, 7:41 pm, liupeng <liup... at 18mail.cn> wrote:
> pattern = re.compile(r'\w+\s*=\s*[0-9]*.[0-9]*\s*')
> lists = pattern.findall(s)
> print lists
> ['a=4 ', 'b=3.4 ', 'c=4.5']
>
> On Wed, Apr 11, 2007 at 06:10:07PM -0700, Qilong Ren wrote:
> > Hi, everyone,
>
> > I am extracting some information from a given string using python RE. The
> > string is ,for example,
> >    s = 'a = 4 b =3.4 5.4 c = 4.5'
> > What I want is :
> >    a = 4
> >     b = 3.4 5.4
> >    c = 4.5
> > Right now I use :
> >    pattern = re.compile(r'\w+\s*=\s*.*?\s+')
> >    lists = pattern.findall(s)
> > It works for the string like 'a = 4 b = 3.4 c = 4.5', but does not work with
> > strings like 'a=4 b=3.4 5.4 c = 4.5'
>
> > Any suggestion?
>
> > Thanks,Qilong
>
> > ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━
> > Don't get soaked. Take a quick peak at the forecast
> > with theYahoo! Search weather shortcut.
> > --
> >http://mail.python.org/mailman/listinfo/python-list
>
>  signature.asc
> 1KDownload

Try this:

import re

s = 'a = 4 b =3.4 5.4 c = 4.5'
r = re.compile("[a-z]+.*?(?=[a-z]|$)" )
l = r.findall(s)
print l
-- 
http://mail.python.org/mailman/listinfo/python-list






       
____________________________________________________________________________________
Be a PS3 game guru.
Get your game face on with the latest PS3 news and previews at Yahoo! Games.
http://videogames.yahoo.com/platform?platform=120121
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070411/7ef5d180/attachment.html>


More information about the Python-list mailing list