What is the difference between a greedy and unwanted regex approach in Ruby?

Can someone help me figure out the difference between the two approaches to regular expressions with some suitable examples?

  • greedy
  • non greedy

thanks

+4
source share
3 answers

Chekout http://www.regular-expressions.info/repeat.html .

Greed refers to the number of times a regex engine will try to match a specific character set. The regular expression expression greed method uses special characters * , + ? and {} .

Consider

 str = "asdfasdfbbbb" r1 = /b/ r2 = /(asdf)*/ r3 = /b{3}/ r4 = /.*/ 

Matching this regular expression from str will result in:

r1 matching "asdfasdf b bbb" (not greedy, trying to combine b only once)
r2 matching <asdfasdf bbbb "(greedy, trying to combine asdf as many times as possible)
r3 match "asdfasdf bbb b" (not greedy, match b exactly 3 times)
r4 matching < asdfasdfbbbb "(ULTRA-greedy, matches almost any character as many times as possible)

Since regex means representing specific text patterns, it doesn't look like greed, it's an issue of approach. Sometimes you will need to match three times foo ( /(foo){3}/ ) or an infinite score ( /(bar)*/ ).

+3
source

In a greedy regex pattern, the approach consumes the maximum characters in the source string. for instance

 textstr = "bcabdcab" textstr.gsub!(/(.*)ab/, "xxx") # this will match whole `bcabdcab` and return `xxx` 

Here * is a greedy quantifier. In a non-greedy approach, the regex engine returns when it meets the eligibility criteria. To make a quantifier not a greedy append ?

 textstr = "bcabdcab" textstr.gsub!(/(.*?)ab/, "xxx") # this will match only `bcab` part and return `xxxdcab` 

gsub returns a copy of str (first argument) with all occurrences of the pattern replaced by the second argument

+7
source
  • * - (0 or more) greedy match
  • + - (1 or more) greedy match
  • *? - (0 or more) unwanted match
  • +? - (1 or more) inanimate correspondence
+2
source

All Articles