Aleksander Mielczarek Aleksander Mielczarek - 17 days ago 6
Java Question

Regex to match string of unique characters

Here is my regular expression:

((GO)( [A-Z])+)


I want every letter to appear at most once, unfortunately it isn't working properly, because this input:

GO A B C C


return true, but should return false.

Answer

You can use this regex:

^(GO(?: ([A-Z])(?!.*\2))+)$

RegEx Demo