Reputation: 25
I'm trying to make a regular expression, but something isn't working for me, the requirements are the following:
This is what I have so far
/^[0-9]{0,2}[a-z][A-Z][0-9]{0,10}$/
Can you guys tell me what I'm doing wrong?
Upvotes: 2
Views: 1270
Reputation: 163217
Your pattern ^[0-9]{0,2}[a-z][A-Z][0-9]{0,10}$
matches 0, 1 or 2 digits at the start.
Then it matches 2 chars [a-z][A-Z]
being a lowercase and an uppercase char A-Z which should be present in the string, and also makes the string length at least 2 chars.
You can make the second digit optional, and use 1 character class for the letters or numbers.
The length then has a minumum of 1, and a maximum of 12.
^(?!\d[a-zA-Z])\d\d?[a-zA-Z0-9]{0,10}$
^
Start of string(?!\d[a-zA-Z])
negative lookahead, assert not a digit followed by a-zA-Z\d\d?
Match 1 or 2 digits[a-zA-Z0-9]{0,10}
Match 0-10 repetitions of any of the listed ranges$
End of stringOr a version withtout a lookahead as suggested by @Scratte in the comments, matching a single digit and optionally a second digit followed by 0-10 repetitions of the listed ranges:
^\d(?:\d[A-Za-z\d]{0,10})?$
Upvotes: 2