I have checked that ^* and ^& match lines beginning by * and &, which I didn't since they are special characters. But ^[ doesn't work. Is this "standard" behavior? Is there any rationale behind this?
sed version used was "GNU sed 4.4".
On
See sed "3.3 Overview of Regular Expression Syntax" documentation.
The & char is not a special regex char, it does not need escaping in a regex pattern. Note that & can be parsed as a special construct in the replacement pattern where is refers to the whole match.
The * is not special when it is at the start in GNU sed (^* is a pattern that matches a * at the start of the string):
POSIX 1003.1-2001 says that
*stands for itself when it appears at the start of a regular expression or subexpression, but many nonGNU implementations do not support this and portable scripts should instead use\*in these contexts.
The [ starts a bracket expression and must have a paired ] to close the expression, hence it is an error.
From POSIX.1-2017:
Reading the POSIX section on BREs, we read:
So to answer the OPs question using the above:
&is not a special character, so^&is expected to work[should always be escaped if it is not used as a bracket expression.*is not special after an initial^when the latter is an anchor.So all observed statements by the OP are therefore valid.
There is however still an interesting paragraph in RE Bracket Expression:
This implies that
]cannot be escaped in a bracket expression. This means:The following work:
but this does not work as expected:
So in a Bracket Expression, dont escape it, but collate it!