re question

Steve Holden steve at holdenweb.com
Thu Nov 2 03:35:53 CET 2006


Schüle Daniel wrote:
> Hello all,
> 
> I didn't found more appropriate news group for
> this question, please let me know if there is ng with
> regular expression as its main topic
> 
> 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.
> 
  >>> target = "<h1>Sample String</h1>"
  >>> greedy = re.compile("<.+>")
  >>> nongreedy = re.compile("<.+?>")
  >>> m1 = greedy.match(target)
  >>> m2 = nongreedy.match(target)
  >>> m1.group(0)
'<h1>Sample String</h1>'
  >>> m2.group(0)
'<h1>'
  >>>

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb       http://holdenweb.blogspot.com
Recent Ramblings     http://del.icio.us/steve.holden




More information about the Python-list mailing list