In standard basic regex, (?\(\d(3}\)[-.]?
means:
a literal left parenthesis
a literal question mark
(start of a group)
a literal character 'd'
a literal left parenthesis
the number '3'
a literal closing brace
(end of group)
a dash or a dot
a question mark
i.e., this will print x
:
echo '(?d(3}-?' |sed 's/(?\(\d(3}\)[-.]?/x/'
You're very likely to want sed -E
to enable extended regular expressions (ERE), and to then use (
and )
for grouping, and \(
and \)
for literal parenthesis.
Also note that \d
is part of Perl regexes, not standard ones, and while GNU sed supports some \X
escapes, they're not standard (and I don't think it supports \d
). Same for \?
, GNU sed supports it in BRE to mean what ?
means in ERE, but it's not standard.
With all that in mind:
$ echo '(123)-456-7890' | sed -E 's/\(?([0-9]{3})\)?[-.]?([0-9]{3})[-.]?([0-9]{4})/\1\2\3/'
1234567890
Though you might almost as well just brute force it and just remove everything but the digits:
$ echo '(123)-456-7890' | sed -e 's/[^0-9]//g'
1234567890
(that would of course also accept stuff like (123)-4.5-6-7a8b9c0
...)
See also:
?
needs backslash quoting. – Ralph Rönnquist Apr 24 '18 at 23:19/
. – Ralph Rönnquist Apr 25 '18 at 00:11