我正在尝试创建一个正则表达式来授权表单中的国际电话号码,同时排除来自法国的电话号码(即以“+33”开头,因为我为此例创建了一个特定的正则表达式)。
此正则表达式应捕获以“+”开头的电话号码,后跟国家/地区代码(1 到 4 位数字)和 4 到 9 位数字,不带空格/破折号/点。
我环顾四周,找到了以下号码,其中包括所有国际电话号码:
(\(?\+[1-9]{1,4}\)?([0-9]{4,11})?)
我想排除法国号码...
[^+33]
但我找不到将其与我当前的正则表达式结合起来的方法。
预先感谢您的帮助。
最佳答案
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.
使用
\A(?!\+33)\+\d{1,3}\d{4,9}\z
参见regex proof .
说明
--------------------------------------------------------------------------------
\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
关于regex - 用于国际电话号码但排除特定国家/地区代码的 Ruby 正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68007253/