Regex Question

Bill Mill bill.mill at gmail.com
Thu Jan 18 17:59:52 EST 2007


Gabriel Genellina wrote:
> At Tuesday 16/1/2007 16:36, Bill  Mill wrote:
>
> > > py> import re
> > > py> rgx = re.compile('1?')
> > > py> rgx.search('a1').groups()
> > > (None,)
> > > py> rgx = re.compile('(1)+')
> > > py> rgx.search('a1').groups()
> >
> >But shouldn't the ? be greedy, and thus prefer the one match to the
> >zero? This is my sticking point - I've seen that plus works, and this
> >just confuses me more.
>
> Perhaps you have misunderstood what search does.
> search( pattern, string[, flags])
>          Scan through string looking for a location where the regular
> expression pattern produces a match
>
> '1?' means 0 or 1 times '1', i.e., nothing or a single '1'.
> At the start of the target string, 'a1', we have nothing, so the re
> matches, and returns that occurrence. It doesnt matter that a few
> characters later there is *another* match, even if it is longer; once
> a match is found, the scan is done.
> If you want "the longest match of all possible matches along the
> string", you should use findall() instead of search().
>

That is exactly what I misunderstood. Thank you very much.

-Bill Mill
bill.mill at gmail.com




More information about the Python-list mailing list