The following regex working as expected other than the case that it's not allowed that all characters are the same characters.
^(?=[A-Z0-9] [ -]?[A-Z0-9] )(?!([A-Z0-9])(?:\1|[ -]){5,10}).{5,10}$
here minimum is 5 characters and the maximum is 10 characters
11114 allowed its minimum length matched as 5 and one charcter is diff so not all same charcters
11111115 allowed as one charcter is different and its more than 5 charcter.
2222222 not allowed as all are same characters
222-22 not allowed as all are same charcters
111-3 allowed as length 5 and one character is different
444-45 allowed as length more than 5
1234565436 allowed as length with in range 5 to 10
CodePudding user response:
There is no need to repeat range quantifier {5,10}
multiple times as that makes changing this regex harder for other cases.
You may use this regex for this:
^(?=.{5,10}$)([A-Z0-9])(?!(?:[ -]?\1) $)[A-Z0-9]*[ -]?[A-Z0-9] $
RegEx Breakup:
^
: Start(?=.{5,10}$)
: Assert that we have 5 to 10 chars till end([A-Z0-9])
: Match a letter or digit and capture in group #1(?!(?:[ -]?\1) $)
: Negative lookahead to fail the match if same captured value is repeated till end[A-Z0-9]*
: Match 0 or more letter or digit[ -]?
: Match optional space or hyphen[A-Z0-9]
: Match 1 or more letter or digit$
: End