problem with re

Fredrik Lundh fredrik at pythonware.com
Thu Sep 6 14:27:45 EDT 2001


Patrick Vrijlandt wrote:
> I do not understand why the 7th regular expression does not give the same
> result as 2-6.

because "|" has lower precedence than just about everything
else; the last pattern either matches 10 digits, *OR* two letter
groups followed by a number.

> mymatch(r'\n'.join([r'(\d{10})|([a-z]{10})', r'([a-z]{10})', r'(\d{10})']))

if you want three groups, remove the parens around the bar:

    mymatch(r'\n'.join([r'(\d{10}|[a-z]{10})', r'([a-z]{10})', r'(\d{10})']))

if you want four groups, you have to add an extra pair of non-capturing
parentheses:

    mymatch(r'\n'.join([r'(?:(\d{10})|([a-z]{10}))', r'([a-z]{10})', r'(\d{10})']))

</F>

<!-- (the eff-bot guide to) the python standard library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->





More information about the Python-list mailing list