正则表达式验证带有字母数字但没有相同字符的字符串

I am working on a regex to validate a string that,

  • contains alphanumeric value
  • does not contain identical characters more than 4 times in a row (like aaaaa or 222222)
  • total length should be between 6 to 15

I am using the following regex, but it doesn't work for the input => String1bbbbb

/^(?=.*[a-z])(?=\S*[A-Z])(?=\S*[0-9])(?!.*[\w{4,}])[a-zA-Z0-9]{6,15}+$/i


if(preg_match('/^(?=.*[a-z])(?=\S*[A-Z])(?=\S*[0-9])(?!.*[\w{4,}])[a-zA-Z0-9]{6,15}+$/i', $string)) {
    echo "Valid String";
} else {
    echo "invalid String";
}

using negative look ahead to validate the identical characters like below

(?!.*[\w{4,}])

Any help would be appreciated.

You can use this regex:

^(?=.{6,15}$)(?:([A-Za-z0-9])\1{0,3}(?!\1))+$

This will make sure length is between 6 and 15, it will capture the character, possibly repeated up to 3 more times and then it will make sure the same character does not get repeated again in the next position. Here you have it in regex101: https://regex101.com/r/aO7pM5/1