How do duplicate backlink clauses work?

Given a regex like

(a|b){2}\1

What will it refer to \1? First capture, last capture or what?

Regex.IsMatch("aba", @"^(?:(a|b){2}\1)$") == False
Regex.IsMatch("abb", @"^(?:(a|b){2}\1)$") == True

Lets me suspect that it matches the last capture. Am I right in this assumption?

+3
source share
1 answer

You understood correctly. Java does this too. Most (if not all) regex engines retain the last capture for repeated groups.

See http://www.regular-expressions.info/brackets.html#repeat for a general description.

See http://msdn.microsoft.com/en-us/library/aa719621%28v=VS.71%29.aspx for confirmation regarding .Net:

"[...], , .

+3

Source: https://habr.com/ru/post/1779055/


All Articles