I need an regular expression that matches A.*C only if there’s no “B” in between. Before and after “A.*C without B in between”, any string is allowed (including A, B and C).
“A”, “B” and “C” are placeholders for longer strings.
So the regex should match ie. “AfooC”, “AfooCbarB”, “A C B A”, but not “AfooBbarC” or “A B C B”.
I think I need a .* somewhere between A and B, so I tried (amongst others) these two:
A.*(?!B).*C doesn’t work, as the .* after A “eats” the B.
A(?!.*B).*C doesn’t work, as it doesn’t match ACB. (This time, the first .* “eats” the “C”).
Possibly I’m missing something obvious – I can’t figure out how to do it.
Thanks for the help, Julian
(Edit: having some formatting troubles…)
The easiest way to achieve this is using lookarounds:
This pattern will match
A, then any number of characters, and thenC. However, because of the negative lookahead on the., the dot will only match if it isn’t going to consumeB.