Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

regex only match when two groups are equal

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 🙂

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading