What is the regex to make sure that a given string contains exactly one uppercase letter? if the string contains more than one i dont want it to be matched.
just 1 Uppercase character
I know the patterns for individual sets namely [a-z]
, [A-Z]
, \d
and _|[^\w]
(I got them correct, didn't I?).
But how do I make them to match only with strings (in java) that only contains 1 uppercase?
CodePudding user response:
You may use this regex with 2 negated character classes:
^[^A-Z]*[A-Z][^A-Z]*$
Above regex will support ASCII upper case letters only. If you want to match unicode letters then use:
^\P{Lu}*\p{Lu}\P{Lu}*$
Here:
\P{Lu}*
: Match 0 or more non-uppercase unicode letters\p{Lu}
: Match an uppercase unicode letter