I've been stuck on this problem for days. I need a regex to validate an Id of this format:
^[0-9]{2}[-]{0,1}[0-9]{7}$
But from this pattern I have to exclude sets like:
- 00-0000000 and 000000000
- 11-1111111 and 111111111
- 22-2222222 and 222222222
- ...
- 99-9999999 and 999999999
Note that 22-2222221
is valid.
Can anyone help me? If the Id is written without the dash, I could use ^(\d)(?!\1{8})\d{8}$ but what if the hyphen is present?
Tried something like
^(\d)(?!\1{8})\d{8}$|^(\d)(?!\1{2})\d{1}-(?!\1{7})\d{7}$
but the second part does not seem to work properly.
Thanks to Wiktor for the solution. How would you adapt it also for pattern below?
^[0-9]{3}[-]{0,1}[0-9]{2}[-]{0,1}[0-9]{4}$
Probably something like this can work: ^(?!(\d)(?:-?\1)*$)\d{3}-?\d{2}-?\d{4}$