regexvalidationnon-repetitive

Regular expression which will match if there is no repetition


I would like to construct regular expression which will match password if there is no character repeating 4 or more times.

I have come up with regex which will match if there is character or group of characters repeating 4 times:

(?:([a-zA-Z\d]{1,})\1\1\1)

Is there any way how to match only if the string doesn't contain the repetitions? I tried the approach suggested in Regular expression to match a line that doesn't contain a word? as I thought some combination of positive/negative lookaheads will make it. But I haven't found working example yet.

By repetition I mean any number of characters anywhere in the string

Example - should not match

aaaaxbc

abababab

x14aaaabc

Example - should match

abcaxaxaz

(a is here 4 times but it is not problem, I want to filter out repeating patterns)


Solution

  • That link was very helpful, and I was able to use it to create the regular expression from your original expression.

    ^(?:(?!(?<char>[a-zA-Z\d]+)\k<char>{3,}).)+$ 
    

    or

    ^(?:(?!([a-zA-Z\d]+)\1{3,}).)+$