Is it possible to make lookahead or lookbehind zero-width assertions in sed? I want to emulate Perl's (?=) and family.

My sed is not GNU sed version 4.0.


No, there aren't. You only have basic regular expressions, which only have character sets (. and […]), repetition (* and \{m,*n*\}), line anchors (^ and $) and grouping \(…\), plus backreferences \N. Some sed implementations have more repetition syntax (\+ and \?), alternation (\|), additional anchors (\b, \B, \<, \>). None that I know of have general zero-width assertions, you need to turn to Perl (or other tools that support PCRE) for that.

Many uses of zero-width assertions can be emulated with general regular expressions, using backreferences. For example, the Perl s/foo(?=bar)/FOO/ can be written s/foo\(bar\)/FOO\1/.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy