[Top][All Lists]

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

Re: unexpected match of s command regexp at ^

From: Davide Brini
Subject: Re: unexpected match of s command regexp at ^
Date: Tue, 28 Sep 2021 20:06:39 +0200

On Tue, 28 Sep 2021 03:20:51 +0200, Christoph Anton Mitterer
<> wrote:

> But why on earth does it (A / A_) match in the beginning?

Because you're doing \(....\)* which matches ZERO or more instances of
what's inside the parentheses. In this particular case, it matches zero
instances. A zero-instances match by convention is present at the
beginning of the string, at the end, and between each pair of characters,
see the following for an example:

$ echo abcd | sed 's/\(foo\)*/X/g'

which brings us to the actual issue: you're not using the /g modifier to the
s/// command, so only the first match is replaced. If you add it to your
expression A above, you get what you expected:

$ printf '%s\n' 't,,,,,,,,single,,,bar=value,foo=,,,,'  |\
 sed 's/\(\(^\|,\+\)\(\(foo\|bar\|baz\)=[^,]*\|single\)\)*//g'


reply via email to

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