regex – What is a greedy regular expression?

Question:

  • What is a Expressão Regular Gulosa ?

  • What differentiates it from the Expressão Regular Não-Gulosa ?

  • Expressões regulares Gulosas consume more resources than Não-Gulosas ?

Answer:

Quantifiers

Specify how many times the previous instance should be captured

Greedy Quantifier

In general, when talking about a greedy quantifier it refers to * , as it represents 0 or infinite times, that is, it will try to capture as much as possible, but if there is nothing to capture this is ok too.

However a greedy quantifier can also be {0,} which has the same effect.

A few more :

+     // captura o máximo possível, mas deve ocorrer ao menos uma vez
{1,}  // tem o mesmo efeito

Thoughts

1. {1,5}  // é um quantificador que vai de 1 a 5
2. {1,80} // é um quantificador que vai de 1 a 80
3. {60,80} // é um quantificador que deve ter no mínimo 60 e vai ate 80

2. would be more greedy than 1. because it matches up to 80 while 1. only with 5, whereas 3. it says it must have at least 60 and can go up to 80 , which would be even more greedy because it specifies a Minimum.

Analogy

Think of a person eating:

  • to 1. says it eats from 1 to 5 kg.
  • to 2. says it eats from 1 to 80 kg.
  • to 3. says she wants to eat from 60 to 80 kg.

non-greedy quantifiers

To make the opposite effect in the greedy quantifier, add the ? , so instead of capturing the maximum it tries to capture the minimum.

In general the *? , which matches the maximum but tries to capture the minimum, in this case the minimum is 0.

a common mistake

`teste .*?` // não faz sentido ter o `.*?`, pois ele não vai capturar nada.

Minimum example

`teste`.macth(/.*?t/) // enquanto o `.*` iria capturar `test`, o mínimo captura apenas `t`

performance

The greedy quantifier is faster, as it eats until there is no more, while the non-greedy quantifier is slower because it always checks if it is as little as possible.

Analogy

Thinking again about people eating:

  • The sweet tooth eats until he has no more food.

  • The non-greedy eats a little checks to see if it's enough, then eats a little more and checks to see if it's enough, and so on.

Scroll to Top