说明
您提供了以下您希望匹配的数字示例。
+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