问题
I want to match a line containing everything except the specified characters [I|V|X|M|C|D|L]
.
new Regex(@"^(.*) is (?![I|V|X|M|C|D|L].*)$")
should match everything except the characters mentioned in the OR
list.
Should match -
name is a
Should not match -
edition is I
回答1:
Try this pattern:
^[^IVXMCDL]*$
This will match the start of the string, followed by zero or more characters other than those specified in the character class, followed by the end of the string. In other words, it will not match any a string which contains those characters.
Also note that depending on how you're using it, you could probably use a simpler pattern like this:
[IVXMCDL]
And reject any string which matches the pattern.
回答2:
You don't need |
in this case, just use ^[^IVXMCDL]*$
^[^IVXMCDL]*$
Debuggex Demo
回答3:
private bool IsValid(String input)
{
bool isValid = false;
// Here we call Regex.Match.
Match match = Regex.Match(input, @"^[^IVXMCDL]*$");
// Here we check the Match instance.
if (match.Success)
{
isValid = true;
}
else
{
isValid = false;
}
return isValid;
}
来源:https://stackoverflow.com/questions/19806835/regex-to-match-everything-except-a-list-of-characters