2

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).)+$"
Abra
  • 19,142
  • 7
  • 29
  • 41
  • It would be best if you would provide example strings that succeed and fail. Try to include a good variety of examples. – WJS Aug 10 '22 at 01:35

1 Answers1

3

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.

bobble bubble
  • 16,888
  • 3
  • 27
  • 46
  • @WJS [Yes, it fails](https://tio.run/##NYxNC4JAFEX3/oqXtJgxHGgtIe2ihUYJQWowqdiYXzhPI8LfPk1ad3Pg3Mst@MDtIn0oJaq26RAKLZhomOUYScmlhCCTCG/DAIC2v5UiAYkcNYZGpFBxUZMTdqLOwxh4l0uqxzBl1l/ABurs@RPE9HzPN6nzn70kZhVremSt7rGcDlnFMblnkphX4i6Y5ZJwa@/2tnc4BvY5psyKojXV7hKvliad30ZjVOoD) maybe you did not escape the `\1`. Further I think with `matches()` in Java you don't need the start and end anchors but I'm not sure about that :) – bobble bubble Aug 10 '22 at 01:44
  • You were right about the escape. It worked. +1 – WJS Aug 10 '22 at 01:49