I’ve spent hours and hours trying to solve this.
Here is my Regex string:
^(?=.{4})(?!.{32})[a-zA-Z0-9_]+((\.(-\.)*-?|-(\.-)*\.?) [a-zA-Z0-9_]+)*$ –
You can test it here
Here is what I tried to acomplish (username validation):
- string length range -> OK
- letters, numbers, _ – . allowed (- and . disallowed at begining and or end of username) -> OK
- need to prevent repeated characters (over 5 characters in line) -> need help!
This:
Use another (negative) lookahead to take care of the third condition. This one will make sure that there is no character, that is followed by itself 4 times.
I also simplified your character classes by using
\wwhich represents[a-zA-Z0-9]. Also, as F.J pointed out, you can combine the first two lookaheads into one: