首页 > 解决方案 > 如何更改此正则表达式以验证没有国际前缀的电话号码?

问题描述

我不太喜欢 REGEX,我正在解决一些问题来调整这个验证电话号码的正则表达式,以适应我的用例。

我有这个带有国际前缀的正则表达式验证电话号码:https ://www.regextester.com/97440

(([+][(]?[0-9]{1,3}[)]?)|([(]?[0-9]{4}[)]?))\s*[)]?[-\s\.]?[(]?[0-9]{1,3}[)]?([-\s\.]?[0-9]{3})([-\s\.]?[0-9]{3,4})

它正确地将字符串验证为:+39 3298494333,但它不会验证表示没有国际前缀的数字的字符串,例如此字符串与我的正则表达式3298494333不匹配

如何更改才能接受没有前缀的电话号码?

标签: regex

解决方案


使用此修复:

(?:(?:\+\(?[0-9]{1,3}|\(?\b[0-9]{4})\)?)?\s*\)?[-\s.]?\(?[0-9]{1,3}\)?[-\s.]?[0-9]{3}[-\s.]?[0-9]{3,4}

证明

解释

--------------------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    (?:                      group, but do not capture:
--------------------------------------------------------------------------------
      \+                       '+'
--------------------------------------------------------------------------------
      \(?                      '(' (optional (matching the most
                               amount possible))
--------------------------------------------------------------------------------
      [0-9]{1,3}               any character of: '0' to '9' (between
                               1 and 3 times (matching the most
                               amount possible))
--------------------------------------------------------------------------------
     |                        OR
--------------------------------------------------------------------------------
      \(?                      '(' (optional (matching the most
                               amount possible))
--------------------------------------------------------------------------------
      \b                       the boundary between a word char (\w)
                               and something that is not a word char
--------------------------------------------------------------------------------
      [0-9]{4}                 any character of: '0' to '9' (4 times)
--------------------------------------------------------------------------------
    )                        end of grouping
--------------------------------------------------------------------------------
    \)?                      ')' (optional (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )?                       end of grouping
--------------------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  \)?                      ')' (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  [-\s.]?                  any character of: '-', whitespace (\n, \r,
                           \t, \f, and " "), '.' (optional (matching
                           the most amount possible))
--------------------------------------------------------------------------------
  \(?                      '(' (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  [0-9]{1,3}               any character of: '0' to '9' (between 1
                           and 3 times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  \)?                      ')' (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  [-\s.]?                  any character of: '-', whitespace (\n, \r,
                           \t, \f, and " "), '.' (optional (matching
                           the most amount possible))
--------------------------------------------------------------------------------
  [0-9]{3}                 any character of: '0' to '9' (3 times)
--------------------------------------------------------------------------------
  [-\s.]?                  any character of: '-', whitespace (\n, \r,
                           \t, \f, and " "), '.' (optional (matching
                           the most amount possible))
--------------------------------------------------------------------------------
  [0-9]{3,4}               any character of: '0' to '9' (between 3
                           and 4 times (matching the most amount
                           possible))


推荐阅读