vb.net 正则表达式匹配除字符列表之外的所有内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19806835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regex to match everything except a list of characters
提问by Soham Dasgupta
I want to match a line containing everything except the specified characters [I|V|X|M|C|D|L].
我想匹配包含除指定字符以外的所有内容的行[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 ORlist.
应该匹配除OR列表中提到的字符之外的所有内容。
Should match -
应该匹配 -
name is a
Should not match -
不应该匹配 -
edition is I
回答by p.s.w.g
Try this pattern:
试试这个模式:
^[^IVXMCDL]*$
This will match the start of the string, followed by zero or more characters other thanthose 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.
并拒绝任何与模式匹配的字符串。
回答by Soner G?nül
You don't need |in this case, just use ^[^IVXMCDL]*$
|在这种情况下你不需要,只需使用^[^IVXMCDL]*$
^[^IVXMCDL]*$


回答by Sudhakar Tillapudi
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;
}

