regex: multiple matching for one string
rurpy at yahoo.com
rurpy at yahoo.com
Wed Jul 22 22:00:42 EDT 2009
On Jul 22, 7:45 pm, "scriptlear... at gmail.com"
<scriptlear... at gmail.com> wrote:
> For example, I have a string "#a=valuea;b=valueb;c=valuec;", and I
> will like to take out the values (valuea, valueb, and valuec). How do
> I do that in Python? The group method will only return the matched
> part. Thanks.
>
> p = re.compile('#a=*;b=*;c=*;')
> m = p.match(line)
> if m:
> print m.group(),
p = re.compile('#a=([^;]*);b=([^;]*);c=([^;]*);')
m = p.match(line)
if m:
print m.group(1),m.group(2),m.group(3),
Note that "=*;" in your regex will match
zero or more "=" characters -- probably not
what you intended.
"[^;]* will match any string up to the next
";" character which will be a value (assuming
you don't have or care about embedded whitespace.)
You might also want to consider using a r'...'
string for the regex, which will make including
backslash characters easier if you need them
at some future time.
More information about the Python-list
mailing list