I have a group of words and another group with a conjunction. I’m looking for a regular expression that matches any single one of those words, demanding the conjunction in between:
- If the words are
(A|B|C)
- and the conjunction is
(&)
- then do match
A & C
,C & B
and evenA & A
- but don’t match
A + C
,A C
orA & D
Practical example: Consider this platform-agnostic regex: /(Huey|Dewey|Louie) and \1/
.
I want it to match “Huey and Louie” or “Dewey and Huey”, but it only matches “Huey and Huey”, because backreferences merely match previously matched texts.
I could repeat myself by using /(Huey|Dewey|Louie) and (Huey|Dewey|Louie)/
but I think there’s a smarter way of re-using capturing groups at a later time. Is that feasible somehow?