I need Regexp to validate string has minimum length 6 and it is contains at least one non-alphanumeric character e.g: "eN%{S$u)"
, "h9YI!>4j"
, "{9YI!;4j"
, "eN%{S$usdf)"
, "dfh9YI!>4j"
, "ghffg{9YI!;4j"
.
我需要Regexp来验证字符串是否具有最小长度6并且它包含至少一个非字母数字字符,例如:“eN%{S $ u)”,“h9YI!> 4j”,“{9YI !; 4j”,“eN %{S $ usdf)“,”dfh9YI!> 4j“,”ghffg {9YI !; 4j“。
This one is working well ^.*(?=.{6,})(?=.*\\d).*$"
but in cases when string does not contain any numbers(e.g "eN%{S$u)"
) it is not working.
这个工作得很好^。*(?=。{6,})(?=。* \\ d)。* $“但是在字符串不包含任何数字的情况下(例如”eN%{S $ u) “)它无法正常工作。
4 个解决方案
#1
11
^(?=.{6})(.*[^0-9a-zA-Z].*)$
We use positive lookahead to assure there are at least 6 characters. Then we match the pattern that looks for at least one non-alphanumeric character ([^0-9a-zA-Z]
). The .*
's match any number of any characters around this one non-alphanumeric character, but by the time we've reached here we've already checked that we're matching at least 6.
我们使用正向前瞻来确保至少有6个字符。然后我们匹配寻找至少一个非字母数字字符([^ 0-9a-zA-Z])的模式。 。*匹配这个非字母数字字符周围的任意数字,但到我们到达这里时,我们已经检查过我们匹配至少6个。
^.*(?=.{6,})(?=.*\\d).*$"
is the regex you tried. Here are some suggestions:
是你试过的正则表达式。以下是一些建议:
- You don't need to match more than 6 characters in the lookahead. Matching only 6 here does no restrict the rest of the regular expression from matching more than 6.
- 您不需要在前瞻中匹配超过6个字符。这里仅匹配6不会限制正则表达式的其余部分匹配超过6。
-
\d
matches a digit, and(?=.*\\d)
is a lookahead for one of them. This is why you are experiencing the problems you mentioned with strings likeeN%{S$u)
. - \ d匹配一个数字,(?=。* \\ d)是其中之一的预测。这就是为什么你遇到了像eN%{S $ u)这样的字符串所提到的问题。
- Even if the point above wasn't incorrect and the regular expression here was correct, you can combine the second lookahead with the
.*
that follows by just using.*\\d.*
. - 即使上面的点不正确并且这里的正则表达式是正确的,你也可以将第二个前瞻与后面的。*结合使用。* \\ d。*。
#2
3
marcog's answer is pretty good, but I'd do it the other way around so that it's easier to add even more conditions (such as having at least one digit or whatever), and I'd use lazy quantifiers because they are cheaper for certain patterns:
marcog的答案非常好,但我会反过来这样做,以便更容易添加更多条件(例如至少有一个数字或其他),并且我会使用惰性量词,因为它们更便宜图案:
^(?=.*?[^0-9a-zA-Z]).{6}
So if you were to add the mentioned additional condition, it would be like this:
因此,如果您要添加上述附加条件,它将是这样的:
^(?=.*?[^0-9a-zA-Z])(?=.*?[0-9]).{6}
As you can see, this pattern is easily extensible. Note that is is designed to be used for checking matches only, its capture is not useful.
如您所见,此模式很容易扩展。注意,它仅用于检查匹配,其捕获无用。
#3
1
Keep it easy.
保持简单。
// long enough and contains something not digit or a-z
x.Length >= 6 && Regex.IsMatch(x, @"[^\da-zA-Z]")
Happy coding.
快乐的编码。
Edit, pure "regular expression":
编辑,纯粹的“正则表达式”:
This first asserts there are 6 letters of anything in the look-ahead, and then ensures that within the look-ahead there is something that is not alpha-numeric (it will "throw away" up to the first 5 characters trying to match).
第一个断言前瞻中有6个字母,然后确保在前瞻中有一些不是字母数字的东西(它会“扔掉”到前5个字符试图匹配) 。
(?=.{6}).{0,5}[^\da-zA-Z]
#4
0
What about that(fixed): ^(?=.{6})(.*[^\w].*)$
怎么样(固定):^(?=。{6})(。* [^ \ w]。*)$
Check this out http://www.ultrapico.com/Expresso.htm it is cool tool which could help you a lot in Regexps learning.
请查看http://www.ultrapico.com/Expresso.htm这是一个很酷的工具,可以帮助你在Regexps学习中做很多事情。
#1
11
^(?=.{6})(.*[^0-9a-zA-Z].*)$
We use positive lookahead to assure there are at least 6 characters. Then we match the pattern that looks for at least one non-alphanumeric character ([^0-9a-zA-Z]
). The .*
's match any number of any characters around this one non-alphanumeric character, but by the time we've reached here we've already checked that we're matching at least 6.
我们使用正向前瞻来确保至少有6个字符。然后我们匹配寻找至少一个非字母数字字符([^ 0-9a-zA-Z])的模式。 。*匹配这个非字母数字字符周围的任意数字,但到我们到达这里时,我们已经检查过我们匹配至少6个。
^.*(?=.{6,})(?=.*\\d).*$"
is the regex you tried. Here are some suggestions:
是你试过的正则表达式。以下是一些建议:
- You don't need to match more than 6 characters in the lookahead. Matching only 6 here does no restrict the rest of the regular expression from matching more than 6.
- 您不需要在前瞻中匹配超过6个字符。这里仅匹配6不会限制正则表达式的其余部分匹配超过6。
-
\d
matches a digit, and(?=.*\\d)
is a lookahead for one of them. This is why you are experiencing the problems you mentioned with strings likeeN%{S$u)
. - \ d匹配一个数字,(?=。* \\ d)是其中之一的预测。这就是为什么你遇到了像eN%{S $ u)这样的字符串所提到的问题。
- Even if the point above wasn't incorrect and the regular expression here was correct, you can combine the second lookahead with the
.*
that follows by just using.*\\d.*
. - 即使上面的点不正确并且这里的正则表达式是正确的,你也可以将第二个前瞻与后面的。*结合使用。* \\ d。*。
#2
3
marcog's answer is pretty good, but I'd do it the other way around so that it's easier to add even more conditions (such as having at least one digit or whatever), and I'd use lazy quantifiers because they are cheaper for certain patterns:
marcog的答案非常好,但我会反过来这样做,以便更容易添加更多条件(例如至少有一个数字或其他),并且我会使用惰性量词,因为它们更便宜图案:
^(?=.*?[^0-9a-zA-Z]).{6}
So if you were to add the mentioned additional condition, it would be like this:
因此,如果您要添加上述附加条件,它将是这样的:
^(?=.*?[^0-9a-zA-Z])(?=.*?[0-9]).{6}
As you can see, this pattern is easily extensible. Note that is is designed to be used for checking matches only, its capture is not useful.
如您所见,此模式很容易扩展。注意,它仅用于检查匹配,其捕获无用。
#3
1
Keep it easy.
保持简单。
// long enough and contains something not digit or a-z
x.Length >= 6 && Regex.IsMatch(x, @"[^\da-zA-Z]")
Happy coding.
快乐的编码。
Edit, pure "regular expression":
编辑,纯粹的“正则表达式”:
This first asserts there are 6 letters of anything in the look-ahead, and then ensures that within the look-ahead there is something that is not alpha-numeric (it will "throw away" up to the first 5 characters trying to match).
第一个断言前瞻中有6个字母,然后确保在前瞻中有一些不是字母数字的东西(它会“扔掉”到前5个字符试图匹配) 。
(?=.{6}).{0,5}[^\da-zA-Z]
#4
0
What about that(fixed): ^(?=.{6})(.*[^\w].*)$
怎么样(固定):^(?=。{6})(。* [^ \ w]。*)$
Check this out http://www.ultrapico.com/Expresso.htm it is cool tool which could help you a lot in Regexps learning.
请查看http://www.ultrapico.com/Expresso.htm这是一个很酷的工具,可以帮助你在Regexps学习中做很多事情。