正则表达式判断字符串是否不包含特定字符
这次问题很简单。
我正在尝试使用正则表达式测试字符串是否不包含字符。 我认为表达式的形式为“[^x]”,其中x是您不想出现的字符,但这似乎不是在职的。
例如,
Regex.IsMatch("103","[^0]")
和
Regex.IsMatch("103&","[^&]")
都返回 true (我期望 false)。
我开始使用 "[^&]"
并认为可能是 & 需要转义为 \&,但这似乎没有什么区别。
有想法吗? 我认为这是一件小事。
另外,我正在使用 .NET,因此请记住这一点。
编辑1:
我发现这个,但它似乎没有回答我遇到的问题。
Edit2:
我想回复 凯文和乔尔的建议。 这些建议确实会更快,但它们无法实现我在这种情况下所需的灵活性,因此,如果您通过搜索找到这个问题,一定要看看他们的答案是否符合您的需求。 在我的例子中,正则表达式被传递到 DataTable 验证方法,该方法循环遍历每一行并验证特定列中该行的内容是否与传入的正则表达式匹配。因为我将重用此方法对于正在验证的其他几个数据表,我想:
- 使用正则表达式启用最广泛的验证,并且
- 始终寻找正匹配(即而不是使用 !Regex.IsMatch(cell, regexvariable),我想依赖 能够使用 Regex.IsMatch(cell, regexvariable) 因为大多数调用此方法的 DataTable 将使用正匹配而不是负匹配。
总是
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 x is the character that you don't want to appear, but that doesn't seem to be working.
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.
Edit1:
I found this, but it doesn't seem to answer the issue I'm having.
Edit2:
I wanted to respond to Kevin and 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:
- 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.
Hopefully that helps.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
你的解决方案只对了一半。 您看到的匹配是针对其他角色的。 你想说的是“嘿!我不想在整个字符串中看到这个字符”。
在这种情况下,你会:
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:
模式 [^0] 将匹配任何非零字符。 在两个示例中,模式都将匹配第一个字符(“1”)。 要测试整个字符串是否不包含零,模式应为“^[^0]*$”。 其内容如下:从字符串的开头开始,匹配任意数量的非零字符,紧接着是字符串的结尾。 任何包含零的字符串都会失败。 任何不包含零的字符串都会通过。
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.
如果您正在寻找字符串中的单个字符,则正则表达式似乎有点大材小用。 为什么不直接使用 .IndexOf 或 .Contains ?
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 ?
解析器到达的第一个字符是“1”,这对于 [^0] 成立,对于 [^&] 也成立,因此在这两个示例中它将返回 true。
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.
你把否定放在了错误的地方。
[^x]
将匹配 x 以外的任何内容。 如果 x 在字符串中,并且还有任何其他字符,则该字符串仍与表达式匹配。 相反,如果 x 存在,您希望匹配 true,然后对函数的结果求反:对于这些简单的示例,普通的 String.IndexOf() 或 String.Contains() 并不是更好的选择。
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:Not that with these simple examples, the normal String.IndexOf() or String.Contains() would be a better choice.
我遇到了这个问题,寻找同样的东西,但针对的是 JavaScript。 上面的表达式在我的情况下不起作用,但我遇到了下面的表达式。 (以防万一其他正在寻找 JavaScript 解决方案的人也在这里结束。)
^((?!0).)*$
这种构造称为 负向预测
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.)
^((?!0).)*$
This construct is called a Negative Lookahead