YANRQ (yet another regex question)

Tim Peters tim.one at comcast.net
Wed Mar 13 19:24:27 EST 2002


[Michael George Lerner]
> I have a regular expression that looks vaguely like this:
>
> r = re.compile(r'''(?P<start>start)(?P<foo>   foo)(?P<end>end)''')
>
> but the middle pattern isn't quite what I want.  I know that foo
> will be a string that is seven characters long and contains 'foo'
> surrounded by spaces.  That is, it could be any of these:
>
> 'foo    '
> ' foo   '
> '  foo  '
> '   foo '
> '    foo'
>
> I'd like to rewrite my regular expression to match any of these,
> and I'd really rather not write it all out like this:
>
> (?P<foo>(foo    | foo   |  foo  |   foo |    foo|)

That will also match an empty string (remove the last vertical bar; ditto
the 2nd left paren).

> Is there some easy way to do this that I've overlooked?

You already found an easy way <wink>.  If you ask whether there's an easier
way, the answer is no.





More information about the Python-list mailing list