我不太喜欢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,但它不验证表示没有国际前缀的数字的字符串,例如,该字符串与regex 3298494333不匹配
怎样才能接受没有前缀的电话号码呢?
发布于 2021-02-05 21:22:17
使用此修补程序:
(?:(?:\+\(?[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))https://stackoverflow.com/questions/66069302
复制相似问题