Is there a regex pattern that will have a list of allowed delimiters but enforce they all must be the same?
For an example, I will use a MAC Address:
xx:xx:xx:xx:xx:xx (allowed)
xx-xx-xx-xx-xx-xx (allowed)
xx:xx-xx:xx-xx:xx (not allowed)
I am currently using this:
[RegularExpression("^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2}){1}$")]
>Solution :
You can use a capture group and a backreference:
^[0-9A-Fa-f]{2}(?=([:-]))(?:\1[0-9A-Fa-f]{2}){5}$
^Start of string[0-9A-Fa-f]{2}Repeat the character class 2 times(?=([:-]))Positive lookahead, assert and capture either:or-(?:\1[0-9A-Fa-f]{2}){5}Repeat 5 times a backreference\1to what is initially captured in group 1 followed by 2 times the character class$End of string
Or with an inline modifier for case insensitive if supported:
(?i)^[0-9a-f]{2}(?=([:-]))(?:\1[0-9a-f]{2}){5}$