Please assist with the proper RegEx matching. Any 2 letters followed by any combination of 6 whole numbers.
These would be valid:
RJ123456
PY654321
DD3212
[a-zA-Z]{2}\d{6}
[a-zA-Z]{2}
means two letters
\d{6}
means 6 digits
If you want only uppercase letters, then:
[A-Z]{2}\d{6}
I depends on what is the regexp language you use, but informally, it would be:
[:alpha:][:alpha:][:digit:][:digit:][:digit:][:digit:][:digit:][:digit:]
where [:alpha:] = [a-zA-Z]
and [:digit:] = [0-9]
If you use a regexp language that allows finite repetitions, that would look like:
[:alpha:]{2}[:digit:]{6}
The correct syntax depends on the particular language you're using, but that is the idea.
You could try something like this:
[a-zA-Z]{2}[0-9]{6}
Here is a break down of the expression:
[a-zA-Z] # Match a single character present in the list below
# A character in the range between “a” and “z”
# A character in the range between “A” and “Z”
{2} # Exactly 2 times
[0-9] # Match a single character in the range between “0” and “9”
{6} # Exactly 6 times
This will match anywhere in a subject. If you need boundaries around the subject then you could do either of the following:
^[a-zA-Z]{2}[0-9]{6}$
Which ensures that the whole subject matches. I.e there is nothing before or after the subject.
or
\b[a-zA-Z]{2}[0-9]{6}\b
which ensures there is a word boundary on each side of the subject.
As pointed out by @Phrogz, you could make the expression more terse by replacing the [0-9]
for a \d
as in some of the other answers.
[a-zA-Z]{2}\d{6}
Depending on if your regex flavor supports it, I might use:
\b[A-Z]{2}\d{6}\b # Ensure there are "word boundaries" on either side, or
(?<![A-Z])[A-Z]{2}\d{6}(?!\d) # Ensure there isn't a uppercase letter before
# and that there is not a digit after
Everything you need here can be found in this quickstart guide.
A straightforward solution would be [A-Za-z][A-Za-z]\d\d\d\d\d\d
or [A-Za-z]{2}\d{6}
.
If you want to accept only capital letters then replace [A-Za-z]
with [A-Z]
.