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

标签 regex ruby

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

相关文章:

ruby gsub 问题

ruby-on-rails - Rails 如何从字符串地址获取经纬度并将其放在谷歌地图上?

ruby-on-rails - 在 Rails 应用程序中使用模块建模

ruby - 如果超时则等待一个元素而不抛出异常

ruby - 缩短 Ruby 中的 if-else 结构

R - 如何提取字符串和空行之间的文本?

sql - Oracle SQL -- 从字符串中删除部分重复项

regex - 匹配有限自然数级数

html - 输入类型编号限制长度并允许+符号

ruby - 使用cucumber/capybara时有没有类似于selenium grid的东西?