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().


--
Gabriel Genellina
Softlab SRL

        

        
                
__________________________________________________ Preguntá. Respondé. Descubrí. Todo lo que querías saber, y lo que ni imaginabas, está en Yahoo! Respuestas (Beta). ¡Probalo ya! http://www.yahoo.com.ar/respuestas
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to