Next: , Previous: , Up: sed regular expressions   [Contents][Index]


5.2 Basic (BRE) and extended (ERE) regular expression

Basic and extended regular expressions are two variations on the syntax of the specified pattern. Basic Regular Expression (BRE) is the default in sed (and similarly in grep). Extended Regular Expression syntax (ERE) is activated by using the -r or -E options (and similarly, grep -E).

In GNU sed the only difference between basic and extended regular expressions is in the behavior of a few special characters: ‘?’, ‘+’, parentheses, braces (‘{}’), and ‘|’.

With basic (BRE) syntax, these characters do not have special meaning unless prefixed backslash (‘\’); While with extended (ERE) syntax it is reversed: these characters are special unless they are prefixed with backslash (‘\’).

Desired patternBasic (BRE) SyntaxExtended (ERE) Syntax
literal ‘+’ (plus sign)
$ echo "a+b=c" | sed -n '/a+b/p'
a+b=c
$ echo "a+b=c" | sed -E -n '/a\+b/p'
a+b=c
One or more ‘a’ characters followed by ‘b’ (plus sign as special meta-character)
$ echo "aab" | sed -n '/a\+b/p'
aab
$ echo "aab" | sed -E -n '/a+b/p'
aab