Regex question about \d

Tim Peters tim.one at comcast.net
Sun Apr 7 14:59:47 EDT 2002


[Gerhard Häring]
> Can somebody please explain this to me:
>
> >>> print re.search(r"\d*", "asdfsfafsw 453 dfsa").group(0)
>
> >>> print re.search(r"\d+", "asdfsfafsw 453 dfsa").group(0)
> 453
>
> Why doesn't the first regex match the digits?

A Python/Perl regexp search matches at the leftmost position it can match.
Any regexp of the form (whatever)* can match an empty string, so searching
for any such regexp matches at the start of the string.  If the only thing
it can match at the start of the string is the empty string, good enough:
that is a match.







More information about the Python-list mailing list