Regex question about \d

Gerhard Häring gerhard at bigfoot.de
Sun Apr 7 15:19:38 EDT 2002


* Tim Peters <tim.one at comcast.net> [2002-04-07 14:59 -0400]:
> [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.

Ah! Ok.

Thanks,

Gerhard
-- 
mail:   gerhard <at> bigfoot <dot> de       registered Linux user #64239
web:    http://www.cs.fhm.edu/~ifw00065/    OpenPGP public key id AD24C930
public key fingerprint: 3FCC 8700 3012 0A9E B0C9  3667 814B 9CAA AD24 C930
reduce(lambda x,y:x+y,map(lambda x:chr(ord(x)^42),tuple('zS^BED\nX_FOY\x0b')))





More information about the Python-list mailing list