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

后端 未结 5 1122
盖世英雄少女心
盖世英雄少女心 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: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.

提交回复
热议问题