Fio
Fio

Reputation: 679

Java regex to find two upper case letters, followed by one space and seven digits

I want to find a regex for the following pattern: "AA XXXXXXX" (two characters, one space and 7 digits).

Example: "AA 1234567".

Now I can't find the answer.

Upvotes: 0

Views: 9005

Answers (1)

guido
guido

Reputation: 19224

The pattern you want is:

[a-zA-Z]{2} [0-9]{7}

exactly two chars (upper or lower case) followed by space followed by exactly 7 digits.

If the characters can only be uppercase like in the sample string:

[A-Z]{2} [0-9]{7}

In Java:

Pattern p = Pattern.compile("[A-Z]{2} [0-9]{7}");
Matcher m = p.matcher("AA 1234567");
boolean b = m.matches();

Upvotes: 9

Related Questions