Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ungreedy regular expressions

Tags:

regex

I am referring a regular expression cheat sheet. It says

{3,5} means 3,4,and 5

{3,5}? means 3,4,5 ungreedy +

what does ungreedy +indicate?

like image 769
Ank Avatar asked May 18 '12 22:05

Ank


People also ask

How do you stop greedy in regex?

You make it non-greedy by using ". *?" When using the latter construct, the regex engine will, at every step it matches text into the "." attempt to match whatever make come after the ". *?" . This means that if for instance nothing comes after the ".

What is a greedy match regex?

Greedy: As Many As Possible (longest match) By default, a quantifier tells the engine to match as many instances of its quantified token or subpattern as possible. This behavior is called greedy. For instance, take the + quantifier.

What is * in Perl regex?

Regular Expression (Regex or Regexp or RE) in Perl is a special text string for describing a search pattern within a given text. Regex in Perl is linked to the host language and is not the same as in PHP, Python, etc. Sometimes it is termed as “Perl 5 Compatible Regular Expressions“.


1 Answers

The quanitifier {3,5}? means that it will try to match 3 occurrences first, then see if the rest of the expression matches. If the rest of the expression fails it will backtrack and try 4, then finally 5.

The greedy version {3,5} will try the matches in the opposite order - longest first.

Note that greediness does not affect whether or not a string matches. It only affects the order in which the engine performs the search, and the contents of the captures if there are capturing groups.

Here's an example that demonstrates the difference. Imagine you have the string aaaaabc.

  • (a{3,5})(\w*) will capture aaaaa and bc. (rubular)
  • (a{3,5}?)(\w*) will capture aaa and aabc. (rubular)
like image 156
Mark Byers Avatar answered Nov 01 '22 21:11

Mark Byers