?Module re documentation bug, error, or misunderstanding?

Fredrik Lundh fredrik at pythonware.com
Sat Jul 27 08:10:26 EDT 2002


David LeBlanc is having a bad RE day ;-)

> > >>> import re
> > >>> re.search("(.+) \1", '55 55')
> > >>>
> > >>> re.search("(.+) (.+)", '55 55')
> > <SRE_Match object at 40098328>
>
> Your quantifiers are greedy, so the first + matches the whole string. "\1"
> is "55 55", not the "55" you expect.

...but when the engine discovers that the string doesn't match, it'll
try some other interpretation of ".+".  And it will keep trying until it
is reasonably sure that there's no way the string could match the
pattern...

> (BTW, it's a good habbit to always make re strings raw: r"".)

Exactly.  Did you try that on the original expression ;-)

> Of course, this might not work either - it's asking a lot to want an "any
> character" specifier with a "one or more" quantifier to magically decide
> that it should stop on a space.

Doesn't stop it from trying, though.  If you read the code, you'll find
that it's a pretty weak kind of magic (for true magic, see tim's sort and
dictionary stuff).

</F>





More information about the Python-list mailing list