Please assist with the proper RegEx matching. Any 2 letters followed by any combination of 6 whole numbers.
These would be valid:
RJ123456
PY654321
DD321234
These would not
DDD12345
12DDD123
Please assist with the proper RegEx matching. Any 2 letters followed by any combination of 6 whole numbers.
These would be valid:
RJ123456
PY654321
DD321234
These would not
DDD12345
12DDD123
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]
.[a-zA-Z]{2}\d{6}
[a-zA-Z]{2}
means two letters\d{6}
means 6 digitsIf 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:
where
[:alpha:] = [a-zA-Z]
and[:digit:] = [0-9]
If you use a regexp language that allows finite repetitions, that would look like:
The correct syntax depends on the particular language you're using, but that is the idea.
You could try something like this:
Here is a break down of the expression:
This will match anywhere in a subject. If you need boundaries around the subject then you could do either of the following:
Which ensures that the whole subject matches. I.e there is nothing before or after the subject.
or
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.Depending on if your regex flavor supports it, I might use: