.net RegEx 判断字符串是否不包含特定字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/198781/
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 tell if a string does not contain a specific character
提问by jerhinesmith
Easy question this time.
这次很简单的问题。
I'm trying to test whether or not a string does not contain a character using regular expressions. I thought the expression was of the form "[^x]" where xis the character that you don't want to appear, but that doesn't seem to be working.
我正在尝试使用正则表达式测试字符串是否不包含字符。我认为表达式的形式为“[^ x]”,其中x是您不想出现的字符,但这似乎不起作用。
For example,
例如,
Regex.IsMatch("103","[^0]")
and
和
Regex.IsMatch("103&","[^&]")
both return true (I would expect false).
两者都返回真(我希望假)。
I started using "[^&]"and thought maybe the & needed to be escaped as \&, but it didn't seem to make a difference.
我开始使用"[^&]"并认为 & 可能需要转义为 \&,但它似乎没有什么区别。
Ideas? I assume it's something small.
想法?我认为这是一件小事。
Also, I'm using .NET, so keep that in mind.
另外,我使用的是 .NET,所以请记住这一点。
Edit1:
编辑1:
I found this, but it doesn't seem to answer the issue I'm having.
我找到了这个,但它似乎没有回答我遇到的问题。
Edit2:
编辑2:
I wanted to respond to Kevinand Joel's suggestions. These suggestions would indeed be faster, but they don't accomplish the flexibility I need in this case, so if you found this question through search, definitely look to see if their answers will fit your needs. In my case, the regular expression is getting passed in to a DataTable validation method that loops through each row and verifies that the contents of that row in a specific column matches the RegEx that is getting passed in. Since I'll be reusing this method for several other DataTables that are being validated, I wanted to:
我想回应凯文和乔尔的建议。这些建议确实会更快,但在这种情况下它们并没有实现我需要的灵活性,所以如果你通过搜索找到了这个问题,一定要看看他们的答案是否符合你的需求。在我的例子中,正则表达式被传递给 DataTable 验证方法,该方法循环遍历每一行并验证特定列中该行的内容是否与传入的 RegEx 匹配。因为我将重用此方法对于正在验证的其他几个数据表,我想:
- Use Regex to enable the widest range of validations, and
- Always look for a positive match (i.e. instead of using !Regex.IsMatch(cell, regexvariable), I wanted to rely on always being able to use Regex.IsMatch(cell, regexvariable) since the majority of DataTables invoking this method will be using the positive match instead of the negative.
- 使用 Regex 启用最广泛的验证,以及
- 始终寻找正匹配(即,而不是使用 !Regex.IsMatch(cell, regexvariable),我想依靠始终能够使用 Regex.IsMatch(cell, regexvariable) 因为调用此方法的大多数 DataTables 将使用正匹配而不是负匹配。
Hopefully that helps.
希望这有帮助。
回答by Jorge Ferreira
Your solution is half right. The match you see is for the other characters. What you want to say is something like "hey! I do not want to see this character in the entire string".
你的解决方案只对了一半。您看到的匹配是针对其他字符的。你想说的是“嘿!我不想在整个字符串中看到这个字符”。
In that case you do:
在这种情况下,您可以:
Regex.IsMatch("103","^[^0]*$")
回答by Ken Paul
The pattern [^0] will match any character that is not a zero. In both of your examples, the pattern will match the first character ("1"). To test whether the entire string contains no zeros, the pattern should be "^[^0]*$". This reads as follows: Start at the beginning of the string, match an arbitrary number of characters which are not zeros, followed immediately by the end of the string. Any string containing a zero will fail. Any string containing no zeros will pass.
模式 [^0] 将匹配任何非零字符。在您的两个示例中,模式将匹配第一个字符(“1”)。要测试整个字符串是否不包含零,模式应为“^[^0]*$”。其内容如下:从字符串的开头开始,匹配任意数量的非零字符,紧接着是字符串的结尾。任何包含零的字符串都将失败。任何不包含零的字符串都会通过。
回答by Kevin
if you are looking for a single character in a string, regex seems like a bit of an overkill. Why not just use .IndexOf or .Contains ?
如果您正在寻找字符串中的单个字符,正则表达式似乎有点矫枉过正。为什么不直接使用 .IndexOf 或 .Contains ?
回答by stephenbayer
The first character the parser reaches is "1", which is true for [^0] and also true for [^&], so therefore it will return true in both of those examples.
解析器到达的第一个字符是“1”,对于 [^0] 和 [^&] 也是如此,因此在这两个示例中它都将返回 true。
回答by Joel Coehoorn
You're putting your negation in the wrong place. [^x]will match anything that isn't x. If x is in the string, the string still matches the expression if there are any other characters as well. Instead, you want to match true if x is there, and then negate the result of the function:
你把你的否定放在错误的地方。 [^x]将匹配任何不是 x 的东西。如果 x 在字符串中,如果还有任何其他字符,该字符串仍然与表达式匹配。相反,如果 x 存在,您希望匹配 true,然后否定函数的结果:
Not Regex.IsMatch("103", "0")
Not Regex.IsMatch("103&", "&")
Not that with these simple examples, the normal String.IndexOf() or String.Contains() would be a better choice.
对于这些简单的例子,普通的 String.IndexOf() 或 String.Contains() 并不是更好的选择。
回答by Brett
I came across this question looking for the same thing but for JavaScript. The expression above did not work in my case, but I came across the below expression which did. (Just in case anybody else looking for a JavaScript solution ends up here too.)
我遇到了这个问题,寻找相同的东西,但对于 JavaScript。上面的表达式在我的情况下不起作用,但我遇到了下面的表达式。(以防万一其他寻找 JavaScript 解决方案的人也在这里结束。)
^((?!0).)*$
^((?!0).)*$
This construct is called a Negative Lookahead
这种构造称为负前瞻

