I’m trying to do a match in regex.
It must match a string of characters of with the following formats:
Start with a C or H, w/ 6 characters following. (Total 7 characters long)
Start with KK and with 8 characters following. (Total 10 characters long)
The field is limited to 10 typed characters. I have the following:
(((C|H).{6})|(KK.{8}))
It matches KK+8 just fine. It fails on C+5 just fine. It succeeds on C+6 just fine. However it succeeds on C+7,C+8 and C+9.
I’m assuming my grouping is wrong, can anyone point out my error?
This is .NET flavored Regex and I’m using Regex.IsMatch to determine if the field exactly matches my regex.
You need to anchor the start (
^) and end ($) of the string:I’ve also trimmed out the unnecessary parentheses and changed
(C|H)to a character class to improve readability.