[Tutor] regular expression question
Kent Johnson
kent37 at tds.net
Tue Apr 28 12:32:34 CEST 2009
2009/4/28 Marek SpociĆski at go2.pl,Poland <marek_sp at 10g.pl>:
>> import re
>> s = 'abc123abc45abc789jk'
>> p = r'abc.+jk'
>> lst = re.findall(p, s)
>> print lst[0]
>
> I suggest using r'abc.+?jk' instead.
>
> the additional ? makes the preceeding '.+' non-greedy so instead of matching as long string as it can it matches as short string as possible.
Did you try it? It doesn't do what you expect, it still matches at the
beginning of the string.
The re engine searches for a match at a location and returns the first
one it finds. A non-greedy match doesn't mean "Find the shortest
possible match anywhere in the string", it means, "find the shortest
possible match starting at this location."
Kent
More information about the Tutor
mailing list