I need a pattern (java regex) that will match international mobile numbers containing 7 to 17 digits with the exception that the first 3 digits can not be (965) consecutively. Thanks in advance.
+1
A:
First (if necessary) remove all non-digits from your string, for example like this:
String sanitized_number = raw_number.replaceAll("\\D+", "");
Then check if the following is True:
boolean foundMatch = sanitized_number.matches("^(?!965)\\d{7,17}$");
Tim Pietzcker
2010-07-15 14:57:47
+1 But don't you need the < for negative look-behind? As in `^(?<!965)\d{7,17}$`
Robusto
2010-07-15 15:01:19
I'm looking ahead, therefore `(?!...)`. No way to look behind at the start of a string :)
Tim Pietzcker
2010-07-15 15:03:27
If you're matching international numbers, would you not allow a leading '+'?
Marty
2010-07-15 15:04:54
@Marty: That `+` is removed in the first step, along with all other non-numbers. I think the requirements are too vague to tell whether that's a Good Thing or not...
Tim Pietzcker
2010-07-15 15:08:48
@Tim: 965 is the dialing code for Kuwait.
Gilbert Le Blanc
2010-07-15 17:04:02
@Tim: Thanks a lot. Its working.@Marty: Yes, the numbers will have a leading '+' or '00'. And I'm gonna match it with '(\+|00)'.
raatprohory
2010-07-16 17:35:37