re question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Schüle Daniel

    #1

    re question

    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.

    Regards, Daniel
  • Steve Holden

    #2
    Re: re question

    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(ta rget)
    >>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

    Comment

    • attn.steven.kuo@gmail.com

      #3
      Re: re question

      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

      Comment

      Working...