regular expression seems broken not greedy when it should be

Discussion in 'Ruby' started by CarlosRivera, Sep 20, 2004.

  1. CarlosRivera

    CarlosRivera Guest

    I am very new to ruby. I was working my way through the tutorials.
    However, it looks like the regular expressions seem broken; however,
    since I am so new to ruby it could be me. It would appear that the
    regular expressions are not greedy.

    I have the following code:

    def showRE(a,re)

    startMarker = "<<<"
    endMarker = ">>>"

    if a =~ re
    "#{$`}#{startMarker}#{$&}#{endMarker}#{$'}"
    else
    "no match for '#{a}'"
    end
    end


    irb(main):012:0> showRE("abc123def", /\d*/)
    => "<<<>>>abc123def"
    irb(main):013:0> showRE("abc123def", /\d+/)
    => "abc<<<123>>>def"
    irb(main):014:0>

    I thought that normally * was greedy and would try to match the largest
    string and not the tiniest. If this is the case the "<<<" and ">>>"
    should be around the number "123" in the output for the first attempt
    similar to the second function call. In the ruby docs, it states at the
    following URL:

    http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html#UL

    The following:

    re ?

    Matches zero or one occurrence of re. The *, +, and {m,n} modifiers are
    greedy by default. Append a question mark to make them minimal.

    Did I misinterpret something? My ruby information:

    ruby 1.8.1 (2003-12-25) [i386-openbsd3.5]
     
    CarlosRivera, Sep 20, 2004
    #1
    1. Advertising

  2. On Monday 20 September 2004 09:54, CarlosRivera wrote:
    [snip]
    > I thought that normally * was greedy and would try to match the largest
    > string and not the tiniest. If this is the case the "<<<" and ">>>"
    > should be around the number "123" in the output for the first attempt

    [snip]

    Regexp usualy follows the left-most-longest rule..
    in your case the longest expression * can find is the empty string.

    --
    Simon Strandgaard
     
    Simon Strandgaard, Sep 20, 2004
    #2
    1. Advertising

  3. CarlosRivera

    CarlosRivera Guest

    Thanks for the help. I blew it. I probably should have been asleep then...
     
    CarlosRivera, Sep 21, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lothar
    Replies:
    12
    Views:
    708
    Fredrik Lundh
    Apr 5, 2005
  2. John Ridley

    Re: re module non-greedy matches broken

    John Ridley, Apr 4, 2005, in forum: Python
    Replies:
    4
    Views:
    341
    =?UTF-8?B?QW5kcsOp?= Malo
    Apr 5, 2005
  3. Wole
    Replies:
    1
    Views:
    117
    Evertjan.
    Feb 25, 2004
  4. Dan Kelly

    Greedy and non greedy quantifiers

    Dan Kelly, Jan 17, 2008, in forum: Ruby
    Replies:
    4
    Views:
    147
    Robert Klemme
    Jan 19, 2008
  5. Matt Garrish

    greedy v. non-greedy matching

    Matt Garrish, Feb 16, 2004, in forum: Perl Misc
    Replies:
    4
    Views:
    164
    Matt Garrish
    Feb 16, 2004
Loading...

Share This Page