Regex question about \d

Steve Holden sholden at holdenweb.com
Mon Apr 15 07:32:43 EDT 2002


"Gerhard Häring" <gh_pythonlist at gmx.de> wrote in message
news:mailman.1018203796.20857.python-list at python.org...
> 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?
>
Because it matches the empty string at the beginning -- the "*" implies zero
or more occurrences, so the pattern matches right at the start, and need
look no further for digits.

regards
 Steve







More information about the Python-list mailing list