Regex c# should not match Boolean Operators
I'm trying to write a regular expression which should be able to identify boolean expressi开发者_JAVA技巧ons.
I need to avoid cases like IF(AND AND AND)
. That is, the name of the variable shouldn't be one of the following operators (OR;AND;XOR)
.
I also tried to use [^(OR)]
but this wasn't helpful. My Regex looks like this:
(?:<Name> [A-Za-z0-9])
Is there any chance to write a Regex which could find a string like OR and then don't match it?
Update:
@Kobi - I tried your solution and it works fine. Is there any other ways to do stuff like this may one for dummies. I want to write readable code
Try a negative lookahead:
(?<Name>\b(?!(?:and|x?or)\b)[A-Za-z0-9]+)
This assumes you're trying to match a single literal, like "IF(this AND this)". The regex checks before taking letters, if all it sees is "and" "or" or "xor", it fails.
Also, make sure you have the right RegexOptions
set - you probably want IgnoreCase
on, and unless you have IgnorePatternWhitespace
the space in your original pattern might fail a match, there is no space in if(var1
, for example.
精彩评论