5

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
anubhava
  • 664,788
  • 59
  • 469
  • 547
jimatwork
  • 63
  • 1
  • 5

2 Answers2

5

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

Community
  • 1
  • 1
Jonny 5
  • 11,051
  • 2
  • 20
  • 42
2
^(?!.*?([A-D]).*?\1)[A-D](?:,[A-D])*$

Try this.See demo.

https://regex101.com/r/uC8uO6/8

vks
  • 63,206
  • 9
  • 78
  • 110