The Regex pattern (foo|bar) baz (foo|bar) will match all four of
foo baz foo
bar baz bar
foo baz bar
bar baz foo
Is there any way to modify this so that only the first two match, when the two groups are equal?
Ideally I’d like to not rewrite things as (foo baz foo|bar baz bar), since baz is a stand-in for a really long and complex expression 🙂
>Solution :
Use \1 for remember, what was in first brackets (\1 is the reference for the first substring written in parentheses).
(foo|bar) baz \1