Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for comma separated characters

Tags:

regex

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
like image 913
jimatwork Avatar asked Feb 18 '15 18:02

jimatwork


1 Answers

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

like image 106
Jonny 5 Avatar answered Sep 21 '22 01:09

Jonny 5