Regex for “Does not contain four or more repeated characters”

后端 未结 5 1090
盖世英雄少女心
盖世英雄少女心 2021-01-15 16:22

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 matc

相关标签:
5条回答
  • 2021-01-15 16:37

    Find char in the group then match repeats

    (.).*(\1{3,})
    
    0 讨论(0)
  • 2021-01-15 16:44

    I used the simple ^(.)(?!\1\1){8,}$ for a 8 or more character that doesn't have any characters that repeat more than twice.

    0 讨论(0)
  • 2021-01-15 16:49

    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))*$
    
    0 讨论(0)
  • 2021-01-15 16:51

    You need to put the .* inside the lookahead:

    (?!.*?(.)\1{3,})
    

    The way you're doing it, the .* consumes the whole string, then the lookahead asserts that there aren't four of the same character after the end of the string, which of course is always true.

    I used a non-greedy star in my lookahead because it seemed more appropriate, but greedy will work too--it just has to be inside the lookahead.

    I'm assuming this is just one of several lookaheads, that being the usual technique for validating password strength in a regex. And by the way, while regex-negation is appropriate, you would have gotten more responses to your question much more quickly if you had used the regex tag as well.

    0 讨论(0)
  • 2021-01-15 16:57

    I think, use this regex .*(.).*\1+.* to matches existd repeated characters. But for four, depend on you.

    Good luck!

    0 讨论(0)
提交回复
热议问题