How Can I forbid repeated characters using regular Expression?
This regular Expression should not allow
1--234567890
1--2345--1212
However following is valid
1-2-3-4-5-6-7-8-9-0
1234567890
The only concern here is minus cannot be entered after each other, so -- should not match
There is no restriction in how many dash are in string.
I’m using C#
Thanks in Advance
Use a negative lookahead:
^((.)(?!\2))+$will match a string with no repeated characters.Alternatively — potentially faster: do a search for
(.)\1which will match a pair of repeated characters.