sed-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Backreferences in character classes?


From: Jack Bates
Subject: Re: Backreferences in character classes?
Date: Sun, 21 Jan 2018 08:55:14 -0700
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0

On 2018-01-20 04:25 PM, Assaf Gordon wrote:
Hello,

On 2018-01-19 02:25 PM, Jack Bates wrote:
Does sed support backreferences in character classes? The following doesn't work for me:

echo "'foo'\"'\"'bar'" | sed "s/\([\"']\)\([^\1]*\)\1/\2/g"
Expected: foo'bar
Actual: foo'"'"'bar

No, back-references do not work inside character classes.
This is not only in sed, but also in perl:

   $ echo "'foo'\"'\"'bar'" | perl -npe "s/([\"'])([^\1]*)\1/\2/g"
   foo'"'"'bar

I would suggest the following:

First,
"sed -E" enables extended regular expression, and then there's no
need to escape the parenthesis. This should be supported on all modern seds (including non-gnu). I will use -E in the examples below.

Second,
Since your character class contains only two characters (single quotes and double quotes), it is rather easy to break it down to a regular expression with alteration:

$ echo "'foo'\"'\"'bar'" | sed -E "s/(\"([^\"]*)\")|('([^']*)')/\2\4/g"
foo'bar

The "trick" is to replace with two back-references (\2 and \4) - one of
them is guaranteed to match (e.g. 'foo' or 'bar') and the other is guaranteed to be empty (because it belongs to the alternate regex part that didn't match).

Hope this helps,

Awesome, very helpful, thank you so much!



reply via email to

[Prev in Thread] Current Thread [Next in Thread]