[Tutor] Question regular expressions - the non-greedy pattern
Alan Gauld
alan.gauld at btinternet.com
Tue Jan 22 01:45:15 CET 2013
Your understanding is flawed.
Try these actions and ponder their significance...
>>> s = '<<html><head><title>Title</title>'
>>> re.search('<.*?>',s).group()
'<<html>'
>>> re.search('<.*>',s).group()
'<<html><head><title>Title</title>'
>>> s = '<<html><head><title>Title-title'
>>> re.search('<.*>',s).group()
'<<html><head><title>'
>>> re.search('<.*t',s).group()
'<<html><head><title>Title-tit'
>>> re.search('<.*?t',s).group()
'<<ht'
>>>
The non greediness applies to what follows the '?' not what precedes it.
That is, it swallows everything up until the last possible matching
pattern by default. Non greedy only swallows up to the first matching
pattern after the ?
HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list