Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

Jun 222010

Capturing group difference in Basic Regular Expressions (BRE) and Extended Regular Expressions (ERE)


One of the very confusion issue related to most GNU/Linux utils which are using regular expression library is they are mostly supporting the Basic Regular Expressions (BRE) by default.


# echo "(foo)" | sed 's/\(foo\)/bar/gi'

You might expect the result is


But the actual result is...


Why the brackets are not replaced? It is because in BRE (which is the default, you don't need to escape, if you escape it, then it will be treated as a capturing group - which is quite non-sense if you came from programming background).

To solve the problem, you can just simply remove the \ escape, or you tell those commands to use ERE, e.g.

# echo "(foo)" | sed -r 's/\(foo\)/bar/gi'

 Leave a Reply



You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>