How to do not allow certain characters duplicated using Regex

Viewed 46

only one letter can be written/selected as value, excepted for I, O, Q, S, X, Y, Z.

I've tried :

String regex="^(?!.*(.).*\1)([ABCDEFGHJKLMNPTURVW])+$|((?!OIXZYQS).)+$"
1 Answers

Not so clear... To only allow upper alphabetic but disallow certain duplicated, e.g.

^(?!.*?([A-HJ-NPRT-W]).*\1)[A-Z]+$

See this demo at regex101

The lookahead at ^ start checks if there is not another of [A-HJ-NPRT-W] by capturing.
You could also do it like this which is slightly less efficient and maybe harder to maintain.

Related