tags:

views:

56

answers:

1

I'm stuck with a regular expression problem.

I've got a string which I need to match. The string always starts with 2 letters, and then is followed by a 6 digit number, e.g.

  • EF123456
  • AB123456

However, there is one combination of letters which I need to ignore. e.g.:

  • XX123456

So I want to write a regular expression to match only the normal format of the strings.

At the moment, I'm having to do:

Pattern pattern = Pattern.compile("[A-Z]{2}[0-9]{6}");
...

if(pattern.matcher(n).matches() && !n.toUpperCase().startsWith("XX")) {
    // do match stuff
}

How can I rewrite my regexp so that I can get rid of the startsWith clause in my code above?

+3  A: 

Use a negative look-ahead:

"(?!XX)[A-Z]{2}[0-9]{6}"

(?!XX) means "don't match if I can match XX at the current position", but it doesn't actually change the current position (so the two characters it tested can still be matched by [A-Z]{2}.)

Blixt
Thanks - that worked. I'll follow your link and read up more.
A_M