Reputation: 7851
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.
However, there is one combination of letters which I need to ignore. e.g.:
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?
Upvotes: 1
Views: 366
Reputation: 50197
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}
.)
Upvotes: 4