On Apr 11, 9:50 pm, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote: > En Wed, 11 Apr 2007 23:14:01 -0300, Qilong Ren <[EMAIL PROTECTED]> > 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 >
Another way is to use split: import re lhs = re.compile(r'\s*(\b\w+\s*=)') for s in [ "a = 4 b =3.4 5.4 c = 4.5", "a = 4.5 b = 'h' 'd' c = 4.5 3.5"]: tokens = lhs.split(s) results = [tokens[_] + tokens[_+1] for _ in range(1,len(tokens), 2)] print s print results -- Regards, Steven -- http://mail.python.org/mailman/listinfo/python-list