re question

attn.steven.kuo at gmail.com attn.steven.kuo at gmail.com
Wed Nov 1 21:38:22 EST 2006


Schüle Daniel wrote:

(snipped)

> I am trying to construct a case where a greedy and
> non greedy operation produce different result.
> I dont see the difference between 'a??b' and 'a?b'
> As far I understand is that ? will first try to match a
> (it's greedy) and only if it fails then it step back
> and lets a unmatched. The other doesn't match a at first,
> only if the pattern fails to match it steps back and match a.
>
> But don't they do eventually the same thing?
> Can someone provide an example where 2 patterns yield
> different results.
>

Perhaps this sheds some light
on the matter:


>>> import re
>>> string = "aaaba"

>>> one =  re.findall(r'a?b?', string)
>>> two =  re.findall(r'a??b?', string)

>>> print one, two

Yields:

['a', 'a', 'ab', 'a', ''] ['', '', '', 'b', '', '']


-- 
Hope this helps,
Steven




More information about the Python-list mailing list