修改的
感谢您提出的建议,使我的问题更加明确:)
比赛正在寻找3个连续的角色: 正则表达式匹配= AaA 653219 正则表达式匹配= AA 555 6219
代码是Asp.Net 4.0。这是整个功能:
public ValidationResult ApplyValidationRules()
{
ValidationResult result = new ValidationResult();
Regex regEx = new Regex(@"^(?=.*\d)(?=.*[a-zA-Z]).{8,20}$");
bool valid = regEx.IsMatch(_Password);
if (!valid)
result.Errors.Add("Passwords must be 8-20 characters in length, contain at least one alpha character and one numeric character");
return result;
}
_______________________________________
我已经尝试了超过3个小时来完成这项工作,参考下面没有运气= /
How can I find repeated characters with a regex in Java?
.net Regex for more than 2 consecutive letters
我已经开始使用8-20个字符a-Z
0-9
:
^(?=.*\d)(?=.*[a-zA-Z]).{8,20}$
As Regex regEx = new Regex(@"^(?=.*\d)(?=.*[a-zA-Z]).{8,20}$");
我尝试添加下面的变体而没有运气:
/(.)\1{9,}/
.*([0-9A-Za-z])\\1+.*
((\\w)\\2+)+".
非常感谢任何帮助!
答案 0 :(得分:32)
正则表达式:
^(?=.{8,20}$)(([a-z0-9])\2?(?!\2))+$
第一个前瞻((?=.{8,20}$)
)检查字符串的长度。第二部分通过以下方式进行双重字符和有效性检查:
(
([a-z0-9]) Matching a character and storing it in a back reference.
\2? Optionally match one more EXACT COPY of that character.
(?!\2) Make sure the upcoming character is NOT the same character.
)+ Do this ad nauseum.
$ End of string.
好。我看到你已经添加了一些额外的要求。我的基本论坛仍然有效,但我们必须给你更多的一步一步的方法。 SO:
^...$
由于显而易见的原因,您的整个正则表达式将被删除为开始和结束字符。
(?=.{n,m}$)
长度检查。将它放在正则表达式的开头,n为最小长度,m为最大长度。
(?=(?:[^REQ]*[REQ]){n,m})
必填字符。将它放在正则表达式的开头,REQ作为您的角色需要N到M的必需角色。你可以放弃(?: ..){n,m}
只需要一个角色。
(?:([VALID])\1?(?!\1))+
你表达的其余部分。用有效字符替换VALID。所以,您的密码正则表达式是:
^(?=.{8,20}$)(?=[^A-Za-z]*[A-Za-z])(?=[^0-9]*[0-9])(?:([\w\d*?!:;])\1?(?!\1))+$
' Splained:
^
(?=.{8,20}$) 8 to 20 characters
(?=[^A-Za-z]*[A-Za-z]) At least one Alpha
(?=[^0-9]*[0-9]) At least one Numeric
(?:([\w\d*?!:;])\1?(?!\1))+ Valid Characters, not repeated thrice.
$
http://regexr.com?34vol这是新的行动。
答案 1 :(得分:1)
收紧匹配标准,因为它太宽泛了;例如,“not A-Za-z”比预期的要多得多。之前的 REGEX 匹配字符串“ThiIsNot”。在大多数情况下,密码只会包含字母数字和标点字符,因此我限制了范围,这使得所有匹配都更加准确。使用字符类以提高人类可读性。添加和排除列表,区分大小写。
^(?=.{8,20}$)(?!(?:.*[01IiLlOo]))(?=(?:[\[[:digit:]\]\[[:punct:]\]]*[\[[:alpha:]\]]){2})(?=(?:[\[[:digit:]\]\[[:punct:]\]\[[:upper:]\]]*[\[[:lower:]\]]){1})(?=(?:[\[[:digit:]\]\[[:punct:]\]\[[:lower:]\]]*[\[[:upper:]\]]){1})(?=(?:[\[[:alpha:]\]\[[:punct:]\]]*[\[[:digit:]\]]){1})(?=(?:[\[[:alnum:]\]]*[\[[:punct:]\]]){1})(?:([\[[:alnum:]\]\[[:punct:]\]])\1?(?!\1))+$
细分:
^(?=.{8,20}$) - Positive lookahead that the string is between 8 and 20 chars
(?!(?:.*[01IiLlOo])) - Negative lookahead for any blacklisted chars
(?=(?:[\[[:digit:]\]\[[:punct:]\]]*[\[[:alpha:]\]]){2}) - Verify that at least 2 alpha chars exist
(?=(?:[\[[:digit:]\]\[[:punct:]\]\[[:upper:]\]]*[\[[:lower:]\]]){1}) - Verify that at least 1 lowercase alpha exists
(?=(?:[\[[:digit:]\]\[[:punct:]\]\[[:lower:]\]]*[\[[:upper:]\]]){1}) - Verify that at least 1 uppercase alpha exists
(?=(?:[\[[:alpha:]\]\[[:punct:]\]]*[\[[:digit:]\]]){1}) - Verify that at least 1 digit exists
(?=(?:[\[[:alnum:]\]]*[\[[:punct:]\]]){1}) - Verify that at least 1 special/punctuation char exists
(?:([\[[:alnum:]\]\[[:punct:]\]])\1?(?!\1))+$ - Verify that no char is repeated more than twice in a row