# Rexular expression quantifiers

**URL:** <https://www.ruby-forum.com/t/rexular-expression-quantifiers/125259>\
**Category:** Ruby\
**Created:** [January 7, 2008, 4:13pm UTC](https://www.ruby-forum.com/t/rexular-expression-quantifiers/125259 "2008-01-07T16:13:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@johnsmith](https://www.ruby-forum.com/u/johnsmith)\
**Post date:** [January 7, 2008, 4:13pm UTC](https://www.ruby-forum.com/t/rexular-expression-quantifiers/125259/1 "2008-01-07T16:13:35Z")

</div>

hi there

just a quick question

what is the difference between greedy and non greedy quantifiers?

cheers

---

<div class="post-metadata">

**Author:** ![johnsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@johnsmith](https://www.ruby-forum.com/u/johnsmith)\
**Post date:** [January 7, 2008, 4:27pm UTC](https://www.ruby-forum.com/t/rexular-expression-quantifiers/125259/2 "2008-01-07T16:27:15Z")

</div>

2008/1/7, Johnathan S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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

---

<div class="post-metadata">

**Author:** ![johnsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@johnsmith](https://www.ruby-forum.com/u/johnsmith)\
**Post date:** [January 7, 2008, 6:50pm UTC](https://www.ruby-forum.com/t/rexular-expression-quantifiers/125259/3 "2008-01-07T18:50:44Z")

</div>

Robert K. pisze:

> 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

---

<div class="post-metadata">

**Author:** ![johnsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@johnsmith](https://www.ruby-forum.com/u/johnsmith)\
**Post date:** [January 7, 2008, 6:56pm UTC](https://www.ruby-forum.com/t/rexular-expression-quantifiers/125259/4 "2008-01-07T18:56:03Z")

</div>

On Jan 7, 2008, at 11:49 AM, Marcin MielÅ¼yÅ„ski wrote:

> > 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

You can get Ruby 1.8 to not backtrack as well:

$ ruby -ve ‘p “aa” =~ /(?\>a+)a/’  
ruby 1.8.6 (2007-09-24 patchlevel 111) [i686-darwin9.1.0]  
nil

James Edward G. II

---

<div class="post-metadata">

**Author:** ![johnsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@johnsmith](https://www.ruby-forum.com/u/johnsmith)\
**Post date:** [January 7, 2008, 8:34pm UTC](https://www.ruby-forum.com/t/rexular-expression-quantifiers/125259/5 "2008-01-07T20:34:07Z")

</div>

> 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(“ **stuff** ”)[0]  
> > =\> “ **stuff** ”  
> > /\<.+?\>/.match(“ **stuff** ”)[0]  
> > =\> “ **”**

 

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

–  
Giles B.

Podcast: [http://hollywoodgrit.blogspot.com](http://hollywoodgrit.blogspot.com)  
Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)  
Tumblelog: [http://giles.tumblr.com](http://giles.tumblr.com)
