regex confusion

Diez B. Roggisch deets_noospaam at
Tue Dec 9 17:16:54 CET 2003

John Hunter wrote:

> In trying to sdebug why a certain regex wasn't working like I expected
> it to, I came across this strange (to me) behavior.  The file I am
> trying to match definitely contains many instances of the letter 'a',
> so I would expect the regex
>   rgxPrev = re.compile('.*?a.*?')

This is a bogus regex - a '*' means "zero or more occurences" for the
expression to the left. '?' means "zero or one occurence" for the exp to
the left. I'm not exactly sure why this is not working, but its definitely
redundant. Eliminiating the redundancy gives you this:

rgxPrev = re.compile('.*a.*')

Works perfect.



More information about the Python-list mailing list