“不包含四个或更多重复字符"的正则表达式; [英] Regex for "Does not contain four or more repeated characters"
问题描述
我对正则表达式的经验有限,我一直在阅读有关否定和否定前瞻等的各种教程和帖子,但似乎没有什么与我的情况完全相符.
我正在尝试在 ASP.NET MVC3
中创建一个属性以解决密码复杂性.部分验证包括最少数量的重复字符.对于当前项目,限制为 3,但我想概括一下.
My experience with regular expressions is limited and I've been reading various tutorials and posts on negation and negative lookahead, etc, but nothing seems to quite match my situation.
I'm trying to create an attribute in ASP.NET MVC3
for password complexity. Part of the validation includes a minimum number of repeated characters. For the current project the limit is 3, but I want to generalize it.
最初,我使用 @"(.)\1{3,}"
来测试 4 个或更多重复字符,然后否定该结果.我现在不能这样做,因为我需要创建一个 ModelClientValidationRegexRule
对象,它只会产生积极的结果.因此,否定必须在正则表达式本身内部完成.我尝试使用负前瞻的每一种方式都失败了,例如@".*(?!(.)\1{3,})"
.
有什么想法吗?
Initially, I was using @"(.)\1{3,}"
to test for 4 or more repeated characters and then negating that result. I can't do that now because I need to create a ModelClientValidationRegexRule
object, which will only work with positive results. As such, the negation must be done inside the regex itself. Every way I've tried to use negative lookahead fails, e.g. @".*(?!(.)\1{3,})"
.
Any ideas?
推荐答案
扭转问题:一个字符最多可以跟 3 个相同的字符.然后它必须跟着别的东西.最后,整个字符串必须由这样的序列组成.在 perl 风格中:
Turn the problem around: a character can be followed by at most 3 of the same. Then it must be followed by something else. Finally, the whole string must consist of sequences like this. In the perl flavor:
^((.)\2{0,3}(?!\2))*$
这篇关于“不包含四个或更多重复字符"的正则表达式;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!