My requirement is to limit the length of the input string to 11 which can be alphanumeric with hyphens. The maximum allowable hyphens are 3 and hyphens shouldn’t be considered in length. Another requirement is to not allow more than 5 continuous repetitive digits.
My Regex is ^(?!.*([0-9])\\1{5})(?=.*([-]){0,3})[a-zA-Z0-9]{11}$
Any help is highly appreciated.
CodePudding user response:
Well, one way is:
^(?!.*?(\d)\1{5})(?=(?:[a-z0-9]-?){11}$)[a-z0-9] (?:-[a-z0-9] ){0,3}$
See an online demo
^
- Start-line anchor;(?!.*?(\d)\1{5})
- Negative lookahead to assert input has no digit that is repeated 6 times;(?=(?:[a-z0-9]-?){11}$)
- Positive lookahead to assert input has 11 alphanumeric characters (with optional hyphens);[a-z0-9] (?:-[a-z0-9] ){0,3}
- 1 Alnum chars followed by a non-capture group (0-3 times) to allow for hyphens;$
- End-line anchor.
Note that it would not allow for hyphens to be at either: start, end or consecutive. Further note that I used the case-insensitive flag.