Strings Containing Neither A Nor B


How to match strings that contain neither A nor B using regex?



Regular Expression does not support NOT operator on a sub pattern. "Neither A nor B" must be implemented by a "Negative Look-Ahead" assertion.

Let's assume that we have following text:

The regular expression pattern will match answers that are neither A nor B, with "m" modifier:

is (?!A|B)(.)\.

(?!A|B) - look ahead for a letter neither A nor B.
(.) - capture that letter.

Click the button to test this regular expression here online:

2016-06-23, 786👍, 0💬