Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression for phone number starting with '00' or '+'

Tags:

java

regex

I've got a regex problem: I'm trying to force a phone number beginning with either "00" or "+" but my attempt doesn't work.

String PHONE_PATTERN = "^[(00)|(+)]{1}[0-9\\s.\\/-]{6,20}$";

It still allows for example "0123-45678". What am i doing wrong?

like image 533
Francis Avatar asked Dec 10 '25 23:12

Francis


2 Answers

Inside character class every character is matched literally, which means [(00)|(+)] will match a 0 or + or | or ( or )

Use this regex:

 String PHONE_PATTERN = "^(?:00|\\+)[0-9\\s.\\/-]{6,20}$";
like image 198
anubhava Avatar answered Dec 12 '25 13:12

anubhava


if you have removed spaces, hyphens and whatever from the number, and you want to catch either +xxnnnnnnnn or 00xxnnnnnnnn where xx is the country code of course and n is the 9 digit number OR 0nnnnnnnnn where a non international number starting with a zero is followed by 9 digits then try this regex

 String PHONE_PATTERN = "^(?:(?:00|\+)\d{2}|0)[1-9](?:\d{8})$"
like image 32
Ultradiv Avatar answered Dec 12 '25 13:12

Ultradiv