正则表达式来判断一个字符串不包含特定字符 [英] RegEx to tell if a string does not contain a specific character

查看:111
本文介绍了正则表达式来判断一个字符串不包含特定字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的问题这个时候。

我想,以测试是否使用常规的前pressions一个字符串不包含的字符。我认为前pression是形式[^ X 的]其中的 X 的是,你不希望出现的字符,但没有按'不像是会奏效。

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.

例如,

Regex.IsMatch("103","[^0]")

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.

另外,我使用的是.NET,所以记住这一点。

Also, I'm using .NET, so keep that in mind.

EDIT1:

我发现<一href="http://stackoverflow.com/questions/42990/regex-to-match-against-something-that-is-not-a-specific-substring">this,但它似乎并没有回答我有问题。

I found this, but it doesn't seem to answer the issue I'm having.

EDIT2:

我想回应<一个href="http://stackoverflow.com/questions/198781/regex-to-tell-if-a-string-does-not-contain-a-specific-character#198793">Kevin和<一href="http://stackoverflow.com/questions/198781/regex-to-tell-if-a-string-does-not-contain-a-specific-character#198796">Joel's建议。这些建议确实是快,但他们没有做到,我需要在这种情况下,灵活性,因此,如果您发现通过搜索这个问题,一定要看看,看看他们的回答将满足您的需求。就我而言,常规的前pression是越来越传递到循环遍历每一行,并验证该行特定列的内容,这是获得传入的正则表达式匹配的DataTable的验证方法,因为我将可重复使用这种方法正在验证其他一些数据表,我想:

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:

  1. 使用正则表达式来实现最广泛的验证,以及
  2. 总是寻找,而不是使用一个积极的匹配(即!Regex.IsMatch(电池,regexvariable),我想依靠总是能够使用Regex.IsMatch(电池,regexvariable),因为大多数的数据表中调用此方法将使用积极的比赛,而不是消极的。

希望帮助。

推荐答案

您的解决方案是说对了一半。你看这场比赛是为其他字符。你想说的是像哎!我不想看到这个字符在整个字符串。

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".

在你这种情况下:

Regex.IsMatch("103","^[^0]*$")

这篇关于正则表达式来判断一个字符串不包含特定字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆