I'm looking for a regex to validate a set of non repeating characters separated by commas.
Given the set of characters like ABCD
Match any comma separated permutation (no repeating characters)
Some matches would be:
A
C
C,B
B,D
D,B,A,C
Some no matches would be:
A,A
ABC
D,B,A,B
This would work but doesn't allow the commas:
\b(?!(?:.\B)*(.)(?:\B.)*\1)[ABCD]+\b
Try if something like this would match your needs:
^(?:([A-D])(?!.*?\1),)*[A-D]$
If there's more than just one [A-D]
, preceding ones must be followed by a comma, captures prior [A-D]
to \1
and checks if not followed by itself using a negative lookahead.
See test at regex101.com; Regex FAQ
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With