Help with regex and optional substring in search string

Timur Tabi timur.tabi at gmail.com
Wed Oct 14 16:51:52 CEST 2009


I'm having trouble creating a regex pattern that matches a string that
has an optional substring in it.  What I'm looking for is a pattern
that matches both of these strings:

Subject: [PATCH 08/18] This is the patch name
Subject: This is the patch name

What I want is to extract the "This is the patch name".  I tried this:

m = re.search('Subject:\s*(\[[\w\s]*\])*(.*)', x)

Unfortunately, the second group appears to be too greedy, and returns
this:

>>> print m.group(1)
None
>>> print m.group(2)
[PATCH 08/18] Subject line
>>>

Can anyone help me?  I'd hate to have to use two regex patterns, one
with the [...] and one without.



More information about the Python-list mailing list