Francis Francis - 2 years ago 91
Java Question

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

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?

Answer Source

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}$";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download