开发者

Regex c# should not match Boolean Operators

开发者 https://www.devze.com 2023-01-18 10:06 出处:网络
I\'m trying to write a regular expression which should be able to identify boolean expressi开发者_JAVA技巧ons.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号