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.
You can use this regex:
^(GO(?: ([A-Z])(?!.*\2))+)$
Your regex is:
GO(?:([A-Z])(?!.*\1))+$
GO
, followed by: .
) subsequent character before the next line break ($
). The key to that last step, which is all you were missing, is the zero-length negative lookahead: (?!.*\1)
You could use the following regex:
^GO (?:([A-Z])(?!.*\1)\s*)*$
It will match anything that:
GO<space>
[A-Z]
) that:
See it working on regex101!
Sample matching cases:
GO A B C
GO ABC
GO A B C G O
Sample non-matching cases:
A B C
GO A A A
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With