Showing results for 
Search instead for 
Did you mean: 

sed extended regexp and subexpressions

Occasional Visitor

sed extended regexp and subexpressions

Has anybody here ever managed to use sed with extended regexp's turned on (-r|--regexp-extended) and been able to use subexpressions \( \) and backreferences \1, \2, \n in substitution commands?

Because extended regexp's use parentheses for grouping expressions, in order to match a literal paren in the pattern space, you have to escape the paren, but esccaped parens are what subexpressions in substitution commands use to denote patterns which may be referenced by backreferences. So, how do you denote subexpressions for backreferences with extended regexp turned on?

Mike Stroyan
Honored Contributor

Re: sed extended regexp and subexpressions

The last paragraph in the DESCRIPTION section of "man 7 regex" states that the \( \) and
\1 \2 subexpression notations are specific
to the 'basic' regular expressions. There
is no equivalent for extended regular