python regular expression help
Paul McGuire
ptmcg at austin.rr.com
Thu Apr 12 01:25:35 EDT 2007
On Apr 11, 11:50 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
wrote:
> En Wed, 11 Apr 2007 23:14:01 -0300, Qilong Ren <qilong_... 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
The pyparsing version is a bit more readable, probably simpler to come
back later to expand definition of varName, for example.
from pyparsing import
Word,alphas,nums,FollowedBy,sglQuotedString,OneOrMore
realNum = Word(nums,nums+".").setParseAction(lambda t:float(t[0]))
varName = Word(alphas)
LHS = varName + FollowedBy("=")
RHSval = sglQuotedString | realNum | varName
RHS = OneOrMore( ~LHS + RHSval )
assignment = LHS.setResultsName("LHS") + '=' +
RHS.setResultsName("RHS")
s = "a = 4.5 b = 'h' 'd' c = 4.5 3.5"
for a in assignment.searchString(s):
print a.LHS, '=', a.RHS
prints:
['a'] = [4.5]
['b'] = ["'h'", "'d'"]
['c'] = [4.5, 3.5]
More information about the Python-list
mailing list