regex - 如何更改此正则表达式以验证没有国际前缀的电话号码?
问题描述
我不太喜欢 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不匹配
如何更改才能接受没有前缀的电话号码?
解决方案
使用此修复:
(?:(?:\+\(?[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))
推荐阅读
- ios - 无法读取规范“GoogleDataTransport (3.2.0)”的许可文件“LICENSE”
- flutter - Flutter - 在不发送电子邮件且不知道用户可能使用哪些客户端的情况下启动电子邮件客户端
- node.js - 如何使用 csurf 保护我的反应应用程序 api?
- python - 保存的 cplex 解决方案的默认位置是什么
- python - Django,使用自定义 QuerySet 搜索,错误地返回空对象
- reactjs - 重新初始化一个反应子表单以供重用
- scala - 如何保存不为空的火花数据框
- python - Python搜索二维数组并返回如果找到并找到索引
- python - Python C Api 静默失败
- scala - Spark - 无法为结构数组创建架构