[英]Can't understand this regex format (Phone number)
我必须根据此正则表达式设置电话号码的格式:
/^\\+([0-9]){1,3}\\.([0-9]\\ ?){6,14}$/
对于我的一生,我无法弄清这下半场。 我尽最大的努力
([0-9]\\\\ ?)
6-14次迭代 如果到目前为止我是对的,我无法弄清楚#4的格式。 还是我完全错了?
假设我有这个电话号码:+0018005551212或18005551212
我将如何形成这个号码? 答案会很好,但解释会更好。 谢谢!!!
不,很有可能\\\\
是一个转义的\\
,因为其中存在另一个将检测转义字符的解释层。 使用双引号字符串时,您经常会在bash
看到它(例如):
pax> echo '\' | sed 's/\\/x/'
x
pax> echo '\' | sed "s/\\\\/x/"
x
pax> echo '\' | sed 's/\/x/'
sed: -e expression #1, char 6: unterminated 's' command
pax> echo '\' | sed "s/\\/x/"
sed: -e expression #1, char 6: unterminated 's' command
使用带双引号的字符串, bash
本身会在sed
看到转义符之前对其进行解释,因此您需要进行两次转义符。 使用单引号时, bash
不会解释字符串中的转义符,因此它们按原样传递给sed
。
这是基于以下事实:虽然国际电话号码可以以+
开头,但我从未见过其中带有反斜杠的号码。
因此规则是:
^ start of string
\\+ a literal +
([0-9]){1,3} 1-3 digits
\\. a literal .
([0-9]\\ ?){6,14} 6-14 digits, each with an optional following apace
$ end of string
这是您当前正则表达式的解释,以及可以匹配的一些示例。
^ assert position at start of a line
\\+ matches the character \ literally one or more times
([0-9]){1,3} capturing group matching a digit between 1 to 3 times
\\ matches the character \ literally
. matches any character (except newline)
( start of another capturing group
[0-9] any digit between 0 to 9
\\ matches the character \ literally
? match a space zero or one time
){6,14} repeat this pattern 6 to 14 times
$ assert position at end of a line
您当前的正则表达式与以下示例匹配(对我而言,这些看起来不像电话号码):
\1\+9\ 9\ 9\ 9\ 9\ 9\
\\\12\79\ 9\ 9\ 9\ 9\ 9\
\123\ 9\ 9\ 9\ 9\ 9\ 9\
\\124\A9\ 9\ 9\ 9\ 9\ 9\
\\124\_9\ 9\ 9\ 9\ 9\9\
\\124\_9\ 9\ 9\ 9\ 9\9\ 9\9\9\ 9\9\9\
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.