Reputation: 81
I need to validate an entry in a form. I was using /^\d{9}[A-Z]$/
to check for 9 digits and a single uppercase character. I was informed that the input could be a single uppercase character and 9 digits OR 9 digits and a single uppercase character, but not both a single uppercase character then 9 digits then a single uppercase character.
Basically either A123456789
or 123456789A
is acceptable, but not A123456789A
What RegEx would I use to verify that there is at least one single uppercase character at either at the beginning or end, but not both... followed or preceded by 9 digits?
Upvotes: 1
Views: 2131
Reputation: 89557
If your regex flavour supports it, you can use a conditional:
^([A-Z])?\d{9}(?(1)|[A-Z])$
Explanation:
^
([A-Z])? # optional capture group
\d{9}
(?(1) # if capture group 1 exist
# then there is nothing
| # ELSE
[A-Z] # there is an uppercase letter
)
$
Upvotes: 0
Reputation: 382150
If you want it in one regex, this should do :
(^\d{9}[A-Z]$)|(^[A-Z]\d{9}$)
Upvotes: 6