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]\\\\ ?)
If I'm right so far, I can't figure out the format of #4. Or am I totally wrong?
Let's say I have this phone number: +0018005551212 or 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:
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. With the single quotes, bash
does not interpret the escapes in the string so they're passed to sed
as is.
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\
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.