[英]Can't understand this regex format (Phone number)
I have to format a phone number according to this regex: 我必须根据此正则表达式设置电话号码的格式:
/^\\+([0-9]){1,3}\\.([0-9]\\ ?){6,14}$/
For the life of me, I can't figure out the second half of this. 对于我的一生,我无法弄清这下半场。 My best efforts have me at
我尽最大的努力
([0-9]\\\\ ?)
([0-9]\\\\ ?)
6-14次迭代 If I'm right so far, I can't figure out the format of #4. 如果到目前为止我是对的,我无法弄清楚#4的格式。 Or am I totally wrong?
还是我完全错了?
Let's say I have this phone number: +0018005551212 or 18005551212 假设我有这个电话号码:+0018005551212或18005551212
How would I form this number? 我将如何形成这个号码? Answer would be great but an explanation would be even better.
答案会很好,但解释会更好。 Thanks!!!
谢谢!!!
No, in all likelihood, the \\\\
is an escaped \\
, needed because there is another interpretive layer in there that will detect escape characters. 不,很有可能
\\\\
是一个转义的\\
,因为其中存在另一个将检测转义字符的解释层。 You often see this in bash
(as one example) when using double-quoted strings: 使用双引号字符串时,您经常会在
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
With the double-quoted string, bash
itself interprets the escapes before sed
ever sees them, so you need to double-escape. 使用带双引号的字符串,
bash
本身会在sed
看到转义符之前对其进行解释,因此您需要进行两次转义符。 With the single quotes, bash
does not interpret the escapes in the string so they're passed to sed
as is. 使用单引号时,
bash
不会解释字符串中的转义符,因此它们按原样传递给sed
。
That's based on the fact that, while international phone numbers can begin with +
, I've never seen one with a backslash in it. 这是基于以下事实:虽然国际电话号码可以以
+
开头,但我从未见过其中带有反斜杠的号码。
So the rules are: 因此规则是:
^ 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
Here is an explanation of your current regex, along with some examples which it can match. 这是您当前正则表达式的解释,以及可以匹配的一些示例。
^ 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
Your current regex matches following examples (which don't look like phone numbers to me): 您当前的正则表达式与以下示例匹配(对我而言,这些看起来不像电话号码):
\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.