I'm attempting to create a regular expression in Java or JavaScript that will match the specified regex pattern only when the total character count is less than 13 (with a minimum of 4 characters).
(COT|MED)[ABCD]?-?[0-9]{1,4}(([JK]+[0-9]*)|(\ DDD)?) ← originally posted
(COT|MED)[ABCD]?-?[0-9]{1,4}(([JK]+[0-9]*)|(\ [A-Z]+)?)
The following values should (and do) match:
MED-123
COTA-1224
MED4
COTB-892K777
MED-33 DDD
MED-234J5678
This value matches, but I don't want it to (I want to only match if there are fewer than 12 characters total):
COT-1111J11111111111111
You can view the examples at
I've attempted to group my expression and use {4,12}
at the end, but this just looks for 4 to 12 instances of the entire expression matching.
I have a feeling like I might be overlooking something simple... Thank you in advance for your assistance!