0

I have text lines that contains:

  • mandatory prefix (some constant text e.g. START),
  • words that must all occur (words and) or one word must occur (words or),
  • mandatory suffix (some constant text e.g. END),

There are examples that should be matched:

STARTcatdogbirdEND
STARTcatEND
STARTdogEND
STARTbirdEND

I can write regexp for and (operator and that's empty in regexp):

START
(?: cat dog bird )
END

Also I can write regexp for or (operator | in regexp):

START
(?: cat | dog | bird )
END

But how to combine the above without repeating all words (imagine there are long words list or complicated regexp expressions).

Here's working solution (and OR or), but with repeating expression:

START
(?:
 (?: cat dog bird ) | 
 (?: cat | dog | bird )
)
END

Online demo: https://regex101.com/r/VRA94J/1

Maybe is there another operator?

START
(?: cat magicOperator dog magicOperator bird )
END

or possibility reusing a fragment of an expression or another way to write this?

mkczyk
  • 2,460
  • 2
  • 25
  • 40

0 Answers0