Yes
The current POSIX standard of sed
does not specify the -E
flag, which enables extended regex (ERE). This alone is enough to conclude that the basic regex (BRE) form 's/\(foo\)\(bar\)/\2\1/'
is the most portable.
However, even if -E
were included sed
's standard—and it will be—, the Regular Expressions document does not define back-references in EREs, so the BRE \(...\) == ERE (...)
association is itself a GNU extension and not guaranteed to be supported by all programs. POSIX Grep, for example, includes the -E
flag, but while each one of
grep 'ee*'
grep -E 'e+'
grep '\(.\)\1'
is compliant,
grep -E '(.)\1'
is not.
Likewise, there are reports that concretely illustrate that BSD does not follow the extension:
[In FreeBSD] sed -E '/(.)\1/d'
removes lines that have a 1
after some other character.
whereas GNU sed
would treat that as an back-reference and remove lines containing two equal and adjacent characters.
+
is ERE but\+
isn't Posixly. These are GNU sed only constructs. – guest_7 Feb 07 '21 at 17:18