I am using Pattern.compile() to find if a text string contains two other strings. But it needs to be in one regex pattern.
For example the string must have “StringOne” and “StringTwo” in it.
I could do Pattern.compile("(StringOne StringTwo|StrinTwo StringOne"), but both strings are quite long and I want to see if I can compress it.
If I do "(StringOne )?StringTwo( StringOne)?" it would match “StringTwo” and “StringOne StringTwo StringOne”.
Use this regex:
This uses two look-aheads anchored to start of input to assert that both strings appear somewhere
Edit:
Added word boundaries
\bto ends of strings to prevent matches of one string within another, although this was not a stated requirement of the question.