Is there a regex to match a string that contains A but does not contain B [duplicate]

You use look ahead assertions to check if a string contains a word or not.

If you want to assure that the string contains “Android” at some place you can do it like this:


You can also combine them, to ensure that it contains “Android” at some place AND “Mobile” at some place:


If you want to ensure that a certain word is NOT in the string, use the negative look ahead:


This would require the word “Android to be in the string and the word “Mobile” is not allowed in the string. The .* part matches then the complete string/row when the assertions at the beginning are true.

See it here on Regexr

Leave a Comment