Using regex, I need to test that a string contains A. But the string cannot contain either B or C.
What is the REGEX syntax?
Using regex, I need to test that a string contains A. But the string cannot contain either B or C.
What is the REGEX syntax?
You could use the following regex:
that will match any word containing A
but not containing C
or B
A regex to match words contaning bar
but not containing car
nor foo
is:
Actually...the following approach isn't too awful:
^(?!.*are)(?!.*how).*(hello)
If you don't want are
or how
but want hello
. The parens around hello
are optional and "captures" just the bit you want instead of the whole string.
Try this:
^(?!.*[BC]).*A.*
I think this is the smallest regex that will do the job.
If you want to check if the whole string contains A and not B or C you might use a negated character class to match not B, C or a newline.
Details
^
Assert position at the start of the line[^BC\n]*
Match zero or more times not a B
or C
or newlineA
Match literally[^BC\n]*
Match zero or more times not a B
or C
or newline$
Assert position at the end of the line