rexular expression quantifiers

Discussion in 'Ruby' started by Johnathan Smith, Jan 7, 2008.

  1. hi there

    just a quick question

    what is the difference between greedy and non greedy quantifiers?

    cheers
    --
    Posted via http://www.ruby-forum.com/.
    Johnathan Smith, Jan 7, 2008
    #1
    1. Advertising

  2. 2008/1/7, Johnathan Smith <>:
    > just a quick question
    >
    > what is the difference between greedy and non greedy quantifiers?


    The greediness.

    Seriously: why don't you just try it out in IRB?

    s=("a"*10 + "b")
    s+=s
    s.scan /\w+b/
    s.scan /\w+?b/

    Cheers

    robert

    --
    use.inject do |as, often| as.you_can - without end
    Robert Klemme, Jan 7, 2008
    #2
    1. Advertising

  3. Robert Klemme pisze:
    > 2008/1/7, Johnathan Smith <>:
    >> just a quick question
    >>
    >> what is the difference between greedy and non greedy quantifiers?

    >
    > The greediness.
    >
    > Seriously: why don't you just try it out in IRB?
    >
    > s=("a"*10 + "b")
    > s+=s
    > s.scan /\w+b/
    > s.scan /\w+?b/
    >


    There's also possessive one (doesn't backtrack)
    ruby 1.9 or jruby 1.1 (trunk)

    irb(main):011:0> "aa" =~ /a++a/
    => nil


    lopex
    Marcin Mielżyński, Jan 7, 2008
    #3
  4. Johnathan Smith

    James Gray Guest

    On Jan 7, 2008, at 11:49 AM, Marcin Miel=C5=BCy=C5=84ski wrote:

    > Robert Klemme pisze:
    >> 2008/1/7, Johnathan Smith <>:
    >>> just a quick question
    >>>
    >>> what is the difference between greedy and non greedy quantifiers?

    >> The greediness.
    >> Seriously: why don't you just try it out in IRB?
    >> s=3D("a"*10 + "b")
    >> s+=3Ds
    >> s.scan /\w+b/
    >> s.scan /\w+?b/

    >
    > There's also possessive one (doesn't backtrack)
    > ruby 1.9 or jruby 1.1 (trunk)
    >
    > irb(main):011:0> "aa" =3D~ /a++a/
    > =3D> nil


    You can get Ruby 1.8 to not backtrack as well:

    $ ruby -ve 'p "aa" =3D~ /(?>a+)a/'
    ruby 1.8.6 (2007-09-24 patchlevel 111) [i686-darwin9.1.0]
    nil

    James Edward Gray II=
    James Gray, Jan 7, 2008
    #4
  5. > what is the difference between greedy and non greedy quantifiers?

    I agree the best way to find out is to audition it in IRB, but just
    for completeness, the difference is that a greedy quantifier will grab
    the largest possible match, and the non-greedy quantifier will grab
    the smallest possible match.

    e.g.:

    >> /<.+>/.match("<b>stuff</b>")[0]

    => "<b>stuff</b>"
    >> /<.+?>/.match("<b>stuff</b>")[0]

    => "<b>"

    The only difference is +?, the non-greedy quantifier; using it, you
    get the first possible match, instead of the biggest. QED.

    --
    Giles Bowkett

    Podcast: http://hollywoodgrit.blogspot.com
    Blog: http://gilesbowkett.blogspot.com
    Portfolio: http://www.gilesgoatboy.org
    Tumblelog: http://giles.tumblr.com
    Giles Bowkett, Jan 7, 2008
    #5
    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. Replies:
    1
    Views:
    383
    Joshua Cranmer
    Sep 9, 2007
  2. NeoGeoSNK
    Replies:
    4
    Views:
    3,667
    Joshua Cranmer
    Mar 3, 2008
  3. Nathan Sokalski

    JavaScript RegExp Quantifiers

    Nathan Sokalski, Jun 11, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    354
    Hans Kesting
    Jun 13, 2008
  4. Dan Kelly

    Greedy and non greedy quantifiers

    Dan Kelly, Jan 17, 2008, in forum: Ruby
    Replies:
    4
    Views:
    142
    Robert Klemme
    Jan 19, 2008
  5. Replies:
    0
    Views:
    138
Loading...

Share This Page