gpt4 book ai didi

regex - 国际电话号码的 Ruby 正则表达式,同时排除特定国家/地区代码

转载 作者:行者123 更新时间:2023-12-04 07:29:50 26 4
gpt4 key购买 nike

我正在尝试创建一个正则表达式来授权表单中的国际电话号码,同时排除来自法国的电话号码(即以“+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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com