【问题标题】:Create phone number regex [duplicate]创建电话号码正则表达式 [重复]
【发布时间】:2016-05-02 21:10:57
【问题描述】:

我正在使用 "^[(\+[0-9]{1,3}\.[0-9]{4,14}(?:x.+)?]$" 正则表达式来验证电话号码. 我希望它也适用于国际号码。 它适用于以下模式: +4454475294x364

我还想添加空格和“-”。 例如:+44 544-75294 x364。

我需要在我的正则表达式中进行哪些更改。

谢谢。

【问题讨论】:

  • 您必须区分北美和世界其他地区的手机验证。根据历史原因,电话号码不同。

标签: java regex validation


【解决方案1】:

说明

您提供了以下您希望匹配的数字示例。

+4454475294x364
+44 544-75294 x364
(123) 555-1212x4567
123-555-1232

正则表达式

此正则表达式将执行以下操作:

  • 匹配您提供格式的国际号码
  • 匹配北美号码
  • 如果电话号码后跟分机号,则捕获该分机号
  • 在明显位置允许使用空格、连字符和括号
  • 这仅限于您在问题中列出的格式

^(?:[+][0-9]{2}\s?[0-9]{3}[-]?[0-9]{3,}|(?:[(][0-9]{3}[)]|[0-9]{3})\s*[-]?\s*[0-9]{3}[-][0-9]{4})(?:\s*x\s*[0-9]+)?

注意:对于 Java,您需要转义正斜杠 \ 以使其看起来像 \\

说明

NODE                     EXPLANATION
----------------------------------------------------------------------
  ^                        the beginning of a "line"
----------------------------------------------------------------------
  (?:                      group, but do not capture:
----------------------------------------------------------------------
    [+]                      any character of: '+'
----------------------------------------------------------------------
    [0-9]{2}                 any character of: '0' to '9' (2 times)
----------------------------------------------------------------------
    \s?                      whitespace (\n, \r, \t, \f, and " ")
                             (optional (matching the most amount
                             possible))
----------------------------------------------------------------------
    [0-9]{3}                 any character of: '0' to '9' (3 times)
----------------------------------------------------------------------
    [-]?                     any character of: '-' (optional
                             (matching the most amount possible))
----------------------------------------------------------------------
    [0-9]{3,}                any character of: '0' to '9' (at least 3
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    (?:                      group, but do not capture:
----------------------------------------------------------------------
      [(]                      any character of: '('
----------------------------------------------------------------------
      [0-9]{3}                 any character of: '0' to '9' (3 times)
----------------------------------------------------------------------
      [)]                      any character of: ')'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      [0-9]{3}                 any character of: '0' to '9' (3 times)
----------------------------------------------------------------------
    )                        end of grouping
----------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
----------------------------------------------------------------------
    [-]?                     any character of: '-' (optional
                             (matching the most amount possible))
----------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
----------------------------------------------------------------------
    [0-9]{3}                 any character of: '0' to '9' (3 times)
----------------------------------------------------------------------
    [-]                      any character of: '-'
----------------------------------------------------------------------
    [0-9]{4}                 any character of: '0' to '9' (4 times)
----------------------------------------------------------------------
  )                        end of grouping
----------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
----------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
----------------------------------------------------------------------
    x                        'x'
----------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
----------------------------------------------------------------------
    [0-9]+                   any character of: '0' to '9' (1 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )?                       end of grouping

示例

使用上面的示例文本

匹配项

[0][0] = +4454475294x364
[1][0] = +44 544-75294 x364
[2][0] = (123) 555-1212x4567
[3][0] = 123-555-1232

【讨论】:

    猜你喜欢
    • 2011-09-04
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    • 1970-01-01
    • 2017-09-23
    相关资源
    最近更新 更多