barlop barlop - 5 months ago 55
Linux Question

how do you specify non-capturing groups in sed?

is it possible to specify non-capturing groups in sed?

if so, how?


The answer, is that as of writing, you can't - sed does not support it. Sed supports BRE, and ERE, but not PCRE.

(Note- One answer points out that BRE is also known as POSIX sed, and ERE is is a GNU extension via sed -r. Point remains that PCRE is not supported by sed. )

Perl will work, for windows or linux

examples here

There is a program albeit for Windows, which can do search and replace on the command line, and does support PCRE. It's called rxrepl. It's not sed of course, but it does search and replace with PCRE support.

C:\blah\rxrepl>echo abc | rxrepl -s "(a)(b)(c)" -r "\1"

C:\blah\rxrepl>echo abc | rxrepl -s "(a)(b)(c)" -r "\3"

C:\blah\rxrepl>echo abc | rxrepl -s "(a)(b)(?:c)" -r "\3"
Invalid match group requested.

C:\blah\rxrepl>echo abc | rxrepl -s "(a)(?:b)(c)" -r "\2"


The author(not me), mentioned his program in an answer over here

It has a really good syntax.

The standard thing to use would be perl.