Question about metacharacter '*'

Rick Johnson rantingrickjohnson at
Sun Jul 6 18:24:18 CEST 2014

On Sunday, July 6, 2014 10:50:13 AM UTC-5, Devin Jeanpierre wrote:
> In related news, the regexp I gave for numbers will match "1a".

Well of course it matched, because your pattern defines "one
or more consecutive digits". So it will match the "1" of
"1a" and the "11" of "11a" likewise.

As an aside i prefer to only utilize a "character set" when
nothing else will suffice. And in this case r"[0-9][0-9]*"
can be expressed just as correctly  (and less noisy IMHO) as

# Note: Grouping used for explicitness.

# Using character sets:
>>> import re
>>>'([0-9][0-9]*)', '1a').groups()
>>>'([0-9][0-9]*)', '11a').groups()
>>>'([0-9][0-9]*)', '111aaa222').groups()

# Same result without charactor sets:
>>>'(\d\d*)', '1a').groups()
>>>'(\d\d*)', '11a').groups()
>>>'(\d\d*)', '111aaa222').groups()

More information about the Python-list mailing list