Reputation: 23
I'm trying to create a regex to authorize international phone numbers in a form, while excluding phone numbers from France (i.e. starting with "+33", since I created a specific regex for this case).
This regex should catch phone numbers starting with '+' followed by the country code (1 to 4 digits) and 4 to 9 digits, with no space/dash/dot.
I've been looking around and came up with the following one, which includes all international phone numbers:
(\(?\+[1-9]{1,4}\)?([0-9]{4,11})?)
I want to exclude French numbers with...
[^+33]
but I can't find a way to combine it with my current regex.
Thanks in advance for your help.
Upvotes: 2
Views: 785
Reputation: 18611
This regex should catch phone numbers starting with '+' followed by the country code (1 to 4 digits) and 4 to 9 digits, with no space/dash/dot.
Use
\A(?!\+33)\+\d{1,3}\d{4,9}\z
See regex proof.
EXPLANATION
--------------------------------------------------------------------------------
\A the beginning of the string
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
\+ '+'
--------------------------------------------------------------------------------
33 '33'
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
\+ '+'
--------------------------------------------------------------------------------
\d{1,3} digits (0-9) (between 1 and 3 times
(matching the most amount possible))
--------------------------------------------------------------------------------
\d{4,9} digits (0-9) (between 4 and 9 times
(matching the most amount possible))
--------------------------------------------------------------------------------
\z the end of the string
Upvotes: 1
Reputation: 1321
Use a negative lookahead at the beginning
(?!^\+33)(\(?\+[1-9]{1,4}\)?([0-9]{4,11}) ?)
(?!^+33) True if not begin by +33
Upvotes: 1