# Greedy and non greedy quantifiers

**URL:** <https://rubytalk.org/t/greedy-and-non-greedy-quantifiers/43836>\
**Category:** ruby-talk\
**Created:** [17 January 2008 10:07 UTC](https://rubytalk.org/t/greedy-and-non-greedy-quantifiers/43836 "2008-01-17T10:07:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dan\_Kelly](https://avatars.discourse-cdn.com/v4/letter/d/b5e925/32.png) [@Dan\_Kelly](https://rubytalk.org/u/Dan_Kelly)\
**Post date:** [17 January 2008 10:07 UTC](https://rubytalk.org/t/greedy-and-non-greedy-quantifiers/43836/1 "2008-01-17T10:07:27Z")

</div>

Im just after a bit of clarification with quantifiers in regular  
expressions.  
I just want to be sure of the differences between the quantifiers. so  
for these various regular expressions,

[a-z]\* - this will match any amount lower case letters  
[a-z]+ - this will match any amount lower case letters  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(whats difference between + and \* in this case?)  
[a-z]+? -

or

\d\* - This will match any amount of digits  
\d\*? - This will only match none or one number

Please can someone offer some clarification Im still unsure of myself  
with these expresions as I'm new to ruby,  
Thanks,  
Dan

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Andrew\_Timberlake](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@Andrew\_Timberlake](https://rubytalk.org/u/Andrew_Timberlake)\
**Post date:** [17 January 2008 10:37 UTC](https://rubytalk.org/t/greedy-and-non-greedy-quantifiers/43836/2 "2008-01-17T10:37:02Z")

</div>

> Im just after a bit of clarification with quantifiers in regular  
> expressions.  
> I just want to be sure of the differences between the quantifiers. so  
> for these various regular expressions,
> 
> [a-z]\* - this will match any amount lower case letters  
> [a-z]+ - this will match any amount lower case letters  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(whats difference between + and \* in this case?)  
> [a-z]+? -
> 
> or
> 
> \d\* - This will match any amount of digits  
> \d\*? - This will only match none or one number
> 
> Please can someone offer some clarification Im still unsure of myself  
> with these expresions as I'm new to ruby,  
> Thanks,  
> Dan

Dan

\* matches 0 or more of the preceding pattern  
+ matches 1 or more of the preceding pattern  
? matches 0 or 1 of the preceding pattern  
{n,m} matches n to m of the preceding pattern

I stand to be corrected but I don't believe that \*? or +? are valid at all.

Andrew Timberlake  
andrew@andrewtimberlake.com  
082 415 8283  
skype: andrewtimberlake

"I have never let my schooling interfere with my education."  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--Mark Twain

---

<div class="post-metadata">

**Author:** ![Radoslaw\_Bulat](https://avatars.discourse-cdn.com/v4/letter/r/71c47a/32.png) [@Radoslaw\_Bulat](https://rubytalk.org/u/Radoslaw_Bulat)\
**Post date:** [17 January 2008 11:17 UTC](https://rubytalk.org/t/greedy-and-non-greedy-quantifiers/43836/3 "2008-01-17T11:17:59Z")

</div>

There are 4 (I hope that didn't miss anything) operators to specify repetitions.

\*, +, ? and {m, n}  
\* - matches 0 or more  
+ - matches 1 or more  
? - matches 0 or 1 (same as {0,1} )  
{m,n} matches m..n

All these operators all greedy (I'll show you examples). To make  
non-greedy operator you must add '?' after operator. So non-greedy  
operators are:  
\*?, +?, ??, {m,n}?  
\*? - matches 0 or more  
+? - matches 1 or more  
?? - matches 0 or 1 (same as {0,1}?)  
{m,n} matches m..n

Maybe '??' looks strange, but all of these are absolutely correct.

So far no differences.

Why we have greedy and non-greedy operators? Because they works in  
different way. Greedy operators tries to match as many character  
(precisely: left expression) as can and if during matching it must  
move back it tries to match fewer characters.

For example:  
puts "aaaa".match(/a\*/) # matches whole string  
puts "aaaa" =~ /a\*?/ # matches no characters (but it success)

Another example:  
puts "aaaabaaaab".match(/.\*b/) # matches whole string  
puts "aaaabaaaab".match(/.\*?b/) # matches "aaab"

I always recommend Jeffrey Friedl's book "Mastering Regular  
Expressions" ([http://regex.info/](http://regex.info/)). After reading this book you will be  
master of regexp ;-).

> **···**
>
> --  
> Radosław Bułat
> 
> [http://radarek.jogger.pl](http://radarek.jogger.pl) - mój blog

---

<div class="post-metadata">

**Author:** ![Dan\_Kelly](https://avatars.discourse-cdn.com/v4/letter/d/b5e925/32.png) [@Dan\_Kelly](https://rubytalk.org/u/Dan_Kelly)\
**Post date:** [17 January 2008 10:46 UTC](https://rubytalk.org/t/greedy-and-non-greedy-quantifiers/43836/4 "2008-01-17T10:46:06Z")

</div>

Andrew Timberlake wrote:

> > or
> > 
> > \d\* - This will match any amount of digits  
> > \d\*? - This will only match none or one number
> > 
> > Please can someone offer some clarification Im still unsure of myself  
> > with these expresions as I'm new to ruby,  
> > Thanks,  
> > Dan
> 
> Dan
> 
> \* matches 0 or more of the preceding pattern  
> + matches 1 or more of the preceding pattern  
> ? matches 0 or 1 of the preceding pattern  
> {n,m} matches n to m of the preceding pattern
> 
> I stand to be corrected but I don't believe that \*? or +? are valid at  
> all.

yeah that helps thanks, I think I was just making things up..

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Radoslaw\_Bulat](https://avatars.discourse-cdn.com/v4/letter/r/71c47a/32.png) [@Radoslaw\_Bulat](https://rubytalk.org/u/Radoslaw_Bulat)\
**Post date:** [17 January 2008 11:19 UTC](https://rubytalk.org/t/greedy-and-non-greedy-quantifiers/43836/5 "2008-01-17T11:19:40Z")

</div>

> puts "aaaabaaaab".match(/.\*?b/) # matches "aaab"

It of course matches "aaaab" (missing one 'a')

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [19 January 2008 20:40 UTC](https://rubytalk.org/t/greedy-and-non-greedy-quantifiers/43836/6 "2008-01-19T20:40:22Z")

</div>

> There are 4 (I hope that didn't miss anything) operators to specify repetitions.
> 
> \*, +, ? and {m, n}  
> \* - matches 0 or more  
> + - matches 1 or more  
> ? - matches 0 or 1 (same as {0,1} )  
> {m,n} matches m..n
> 
> All these operators all greedy (I'll show you examples). To make  
> non-greedy operator you must add '?' after operator. So non-greedy  
> operators are:  
> \*?, +?, ??, {m,n}?  
> \*? - matches 0 or more  
> +? - matches 1 or more  
> ?? - matches 0 or 1 (same as {0,1}?)  
> {m,n} matches m..n
> 
> Maybe '??' looks strange, but all of these are absolutely correct.
> 
> So far no differences.
> 
> Why we have greedy and non-greedy operators? Because they works in  
> different way. Greedy operators tries to match as many character  
> (precisely: left expression) as can and if during matching it must  
> move back it tries to match fewer characters.
> 
> For example:  
> puts "aaaa".match(/a\*/) # matches whole string  
> puts "aaaa" =~ /a\*?/ # matches no characters (but it success)

But can match more if it needs to to make the match a success:

irb(main):005:0\> /a\*?/.match("aaab").to\_a  
=\> [""]  
irb(main):006:0\> /a\*?b/.match("aaab").to\_a  
=\> ["aaab"]

> Another example:  
> puts "aaaabaaaab".match(/.\*b/) # matches whole string  
> puts "aaaabaaaab".match(/.\*?b/) # matches "aaab"
> 
> I always recommend Jeffrey Friedl's book "Mastering Regular  
> Expressions" ([http://regex.info/\](http://regex.info/%5C)). After reading this book you will be  
> master of regexp ;-).

Definitively worth reading!

Cheers

&nbsp;&nbsp;robert

> **···**
>
> On 17.01.2008 12:17, Rados?aw Bu?at wrote:
