How can I do an exact match exclusion on a full string with a .NET regular expression?
I need a .NET regular expression that matches anything other than the exact full string match specified. So basically:开发者_如何学JAVA
^Index$
... is the only exclusion I care about. Strings can start with, finish with or contain "Index", but not match exactly.
The answer must be via the pattern itself, as I am passing an argument to a third-party library and do not have control over the process other than via the Regex pattern.
This should do the trick:
^(?!Index$)
If a regular expression is a must,
Match match = Regex.Match(input, @"^Index$");
if (!match.Success){
// Do something
}
And with a horrible way
Match match = Regex.Match(input, @"^(.*(?<!Index)|(?!Index).*)$");
if (match.Success){
// Do something
}
Note: the second one is not tested, and the regular expression engine needs to support full look ahead and look behind.
Use if (!r.Match("^Index$").Success) ...
.
You can check !regex.Match.Success
.
精彩评论