Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex capture order: wrong alternative matched after greedy pattern

Tags:

c#

regex

I have this pattern:

(\w+)(sin|in|pak|red)$

And the replacement pattern is this:

$1tak

The problem is that this word:

setesin

will be transformed to:

setestak

instead of

setetak

For some reason, in always takes precedence to sin in the pattern.

How can I enforce the pattern to follow that order?

like image 455
Cornwell Avatar asked Nov 28 '16 13:11

Cornwell


People also ask

Does order matter in regex?

The order of the characters inside a character class does not matter. The results are identical. You can use a hyphen inside a character class to specify a range of characters. [0-9] matches a single digit between 0 and 9.

What is a regex greedy and lazy match?

'Greedy' means match longest possible string. 'Lazy' means match shortest possible string.

What is lazy quantifier in regex?

Lazy: As Few As Possible (shortest match) Since the * quantifier allows the engine to match zero or more characters, \w*? E tells the engine to match zero or more word characters, but as few as needed—which might be none at all—then to match an E.

Which of the following are greedy match quantifiers?

By default quantifiers like * and + are "greedy", meaning that they try to match as much of the string as possible.


1 Answers

Use a lazy quantifier:

(\w+?)(sin|in|pak|red)$
    ^

See the regex demo

The \w+ contains a greedy quantifier that: 1) grabs as many chars as it can (and note it can match s, i, all letters, digits and underscores) and then backtracks (yielding one char after another moving from right to left), trying to accommodate for the subsequent patterns. Since the in is found first, it is matched, and the whole group is considered matched, the regex goes on to check the end of string with $. A lazy quantifier will have the regex engine skip the \w+? after matching 1 word char, and other patterns will be tried, moving from left to right.

like image 101
Wiktor Stribiżew Avatar answered Oct 13 '22 01:10

Wiktor Stribiżew