Denis Bitouzé
Denis Bitouzé

Reputation: 654

Correct (?) regex not understood by sed

According to https://regex101.com/r/NLSymf/3, the following regex:

\[\[(foo)([^\]]+)\]\]

(full) matches the string [[foo>test1|test2]], but this seems to not be understood by sed, since:

echo "[[foo>test1|test2]]" | sed -E -e '/\[\[(foo)([^\]]+)\]\]/d'

(which should return an empty string) returns:

[[foo>test1|test2]]

What is the regex that matches [[foo>test1|test2]] from sed's point of view?

Upvotes: 2

Views: 79

Answers (1)

oguz ismail
oguz ismail

Reputation: 50775

The backslash character loses its escaping capability within a bracket expression. And stray closing brackets in a RE need not be escaped, that's why grep doesn't fail the first pipeline below. See RE Bracket Expression for reference.

$ echo 'a]' | grep -Eo '[^\]]'
a]
$ echo 'a]' | grep -Eo '[^]]'
a

The correct regex would be:

\[\[(foo)([^]]+)]]

Upvotes: 3

Related Questions