Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Greedy and non greedy quantifiers

Reply
Thread Tools

Greedy and non greedy quantifiers

 
 
Dan Kelly
Guest
Posts: n/a
 
      01-17-2008
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
(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/.

 
Reply With Quote
 
 
 
 
Andrew Timberlake
Guest
Posts: n/a
 
      01-17-2008
> 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
> (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
http://www.velocityreviews.com/forums/(E-Mail Removed)
082 415 8283
skype: andrewtimberlake

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




 
Reply With Quote
 
 
 
 
Dan Kelly
Guest
Posts: n/a
 
      01-17-2008
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/.

 
Reply With Quote
 
Radosław Bułat
Guest
Posts: n/a
 
      01-17-2008
> puts "aaaabaaaab".match(/.*?b/) # matches "aaab"

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

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      01-19-2008
On 17.01.2008 12:17, Rados?aw Bu?at wrote:
> 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/). After reading this book you will be
> master of regexp .


Definitively worth reading!

Cheers

robert

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Resolving Quantifiers of Sub-Expression Quantifiers: ie: /( ( ( foo\w* )+? | ( fle\w+)* ){0,5} ) )? bar/xg; ? sln@netherlands.com Perl Misc 0 02-18-2009 12:48 AM
JavaScript RegExp Quantifiers Nathan Sokalski ASP .Net 2 06-13-2008 07:51 AM
Question about Quantifiers in java Regular expression NeoGeoSNK Java 4 03-03-2008 09:49 PM
With regex, accessing multiple groups under quantifiers valan.wood@gmail.com Java 1 09-09-2007 10:26 PM
greedy v. non-greedy matching Matt Garrish Perl Misc 4 02-16-2004 03:25 PM



Advertisments