# What is a greedy Regular Expression?

19
• What is a `Expressão Regular Gulosa` ?

• What sets it apart from `Expressão Regular Não-Gulosa` ?

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

asked by anonymous 23.10.2015 / 12:49

19

## Quantifiers

Specify how many times the previous instance should be captured

## Gulous Quantifier

In general, when talking about a greasy quantifier, it refers to `*` , because it represents 0 or infinite times, that is, it tries to capture as much as possible, but if there is no catch, this% p>

However, a greedy quantifier can also be `ok` which has the same effect.

Some 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
``````

`{0,}` would be more greedy than `2.` because it matches with 80% while `1.` with only 5, `1.` says that it must have at least `3.` can go up to `60` , which would be greedy even though it specifies a minimum.

### Analogy

Think of a person eating:

• a `80` says that he eats 1 to 5 kg.
• a `1.` says that he eats from 1 to 80 kg.
• a `2.` says he wants to eat from 60 to 80 kg.

## Non-greasy quantifiers

In order to do the opposite effect in the greedy quantifier, the `3.` is added, so instead of capturing the maximum it tries to capture the minimum.

In general, it refers to `?` , which marries with the maximum, but tries to capture the minimum, in this case the minimum is 0.

### A common error

``````'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, because it is eating until it does not give more, whereas the non-greedy is slower because it always verifies if it is the least possible.

### Analogy

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

23.10.2015 / 13:34
14

Sweet : search for the last occurrence by the end of the string, ie get as much as possible.

Eg:

Regex : apple. *

Input : orange watermelon apple

What the regex will pick up is: 'orange watermelon apple'

Non-greasy search until the first occurrence, that is, get as few as possible, examples: `?` , `{1}`

Eg:

Regex : apple. *?

Input : orange watermelon apple

What the regex will pick up is: 'apple'

`Expressões regulares Gulosas` consume more resource than `Não-Gulosas?`

By analyzing the quick explanation above, we can conclude that the use of a greedy regular expression in longer texts will consume more processing and memory.

23.10.2015 / 12:58
8

A greedy regex tries to match the specified pattern as many times as possible, the asterisk is used to set this `*` .

A non-greedy expression matches the minimum possible text by finding the first occurrence of the pattern it stops.

Some greedy expressions tend to consume more resources because they analyze the text against the non-greasy ones that analyze part of it.

23.10.2015 / 13:19