regex: multiple matching for one string

Bill Davy Bill at
Thu Jul 23 11:44:50 EDT 2009

"Mark Lawrence" <breamoreboy at> wrote in message 
news:mailman.3588.1248355389.8015.python-list at
> scriptlearner at 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,
> IMHO a regex for this is overkill, a combination of string methods such as 
> split and find should suffice.
> Regards.

For the OP, it can be done with regex by grouping:

p = re.compile(r'#a=(*);b=(*);c=(*);')
m = p.match(line)
       if m:
             print, has valuea in it, etc.

But this may not be the best way, but it is reasonably terse. 

More information about the Python-list mailing list