Search code examples
javaregexunique

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.


Solution

  • You can use this regex:

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

    RegEx Demo