regex confusion

Peter Otten __peter__ at
Tue Dec 9 17:51:01 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.*?')
> to match it the string contents of the file.  But it doesn't.  Here is

> I read the regex to mean non-greedy match of anything up to an a,
> followed by non-greedy match of anything following an a, which this
> file should match.

There is a nice example where non-greedy regexes are really useful in A. M.
Kuchling's Regex Howto ( 

> Or am I insane?

This may be off-topic, but the easiest if not fastest way to find multiple
occurences of a string in a text is:

>>> import re
>>> r = re.compile("a")
>>> for m in r.finditer("abca\na"):
...     print m.start()


More information about the Python-list mailing list