Regular expressions are easy. They are part of everyday life, and for the most part you don't have to think about them. However, there are some minor details that you may not remember from the top of your head, so here is a brief overview of the different kinds of regular expressions and which tool that goes with which.
Types of regular expressions
There are three kinds of regular expressions: Basic Regular Expressions, Extended Regular Expressions and Perl regular expressions.
Meta characters
Basic Regular Expressions | Extended Regular Expressions | |
---|---|---|
Collation | [==] [::] [..] | [==] [::] [..] |
Escape | \ | \ |
Bracket | [] | [] |
Subexpression & back-reference | \( \) \n | |
Duplication1 | * \{m,n\} | * + ? {m, n} |
Anchoring | ^ $ | ^ $ |
Grouping | () | |
Alternation | | |
1The Open Group refers to this with the somewhat
confusing terms single-character-BRE duplication
and single-character-ERE duplication
,
even though the latter can be used for
matching the duplication of a preceding group.
The potential gotchas to be encountered are due to the parentheses and curly braces. The following session may be used as an explanation. Notice for example how both the BRE foo() and the ERE foo\(\) matches only the first line of the file, but for different reasons. It is also worth noticing that back references are not supported by Extended Regular Expressions.
$ more foo
foo()
foo(bar)
$ grep -G 'foo()' foo
foo()
$ grep -G 'foo\(\)' foo
foo()
foo(bar)
$ grep -G 'foo(bar)' foo
foo(bar)
$ grep -G 'foo\(bar\)' foo
$ grep -E 'foo()' foo
foo()
foo(bar)
$ grep -E 'foo\(\)' foo
foo()
$ grep -E 'foo(bar)' foo
$ grep -E 'foo\(bar\)' foo
foo(bar)
$
Tools that use regular expressions
Tool | Regular Expressions | Comment |
---|---|---|
ed | BRE | // is equivalent to the last regular expression |
ex | BRE |
// is equivalent to the last regular expression \< and \> matches the beginning and end of a word ~ matches the replacement of the last substitute command |
grep | BRE | The -E option turns on ERE |
egrep | ERE | This program is marked as legacy in SUSv2 |
BSD grep | BRE | The -E option turns on ERE |
GNU grep | BRE |
The -E option turns on ERE The -P option turns on PCRE {,m} matches at most m times |
sed | BRE |
The -E option turns on ERE // is equivalent to the last regular expression \n matches a newline embedded in the pattern space |
awk | ERE | C language conventions and a few others (notably \ddd) may be used |
gawk | ERE | A bunch of gawk-specific operators are supported |
mawk | ERE | The option -W traditional turns interval expressions (and others?) off |
vi | BRE | The same enhanced regular expressions as ex |
vim | Read The User manual and The Reference manual |