[英]Regular expression to match anything except binary numbers in C#?
我想检查字符串是否只包含0s
和1s
,没有别的。 如果它包含除0
和1
之外的任何其他内容,我想抓住它。 即使单个字符与0
或1
不同。
我写了下面的正则表达式,但它似乎没有捕捉到任何东西。
private static int bin(string binaryNumber) {
Regex rgx = new Regex(@"^[a-zA-Z2-9\p{P}\p{S}\s,]*$");
if (rgx.IsMatch(binaryNumber)) {
Console.WriteLine("Binary number should include only 0 and 1");
}
// rest removed for brevity
}
有任何想法吗?
尝试这个
Regex rgx = new Regex(@"[^01]+$");
Regex rgx = new Regex(@"^[01]+$");
而不是尝试反转它。
检查 binaryNumber 是否仅与二进制数的 Regex 不匹配。
private static int bin(string binaryNumber) {
Regex rgx = new Regex(@"^[01]+$");
if (!rgx.IsMatch(binaryNumber)) {
Console.WriteLine("Binary number should include only 0 and 1");
}
// rest removed for brevity
}
尝试
^[01]*$
接受任意数量的数字,包括无。
或者
^[01]+$
接受一位或多位数字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.