Regular Expressions

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 ExpressionsExtended 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

ToolRegular ExpressionsComment
edBRE// is equivalent to the last regular expression
exBRE // is equivalent to the last regular expression
\< and \> matches the beginning and end of a word
~ matches the replacement of the last substitute command
grepBREThe -E option turns on ERE
egrepEREThis program is marked as legacy in SUSv2
BSD grepBREThe -E option turns on ERE
GNU grepBRE The -E option turns on ERE
The -P option turns on PCRE
{,m} matches at most m times
sedBRE The -E option turns on ERE
// is equivalent to the last regular expression
\n matches a newline embedded in the pattern space
awkEREC language conventions and a few others (notably \ddd) may be used
gawkEREA bunch of gawk-specific operators are supported
mawkEREThe option -W traditional turns interval expressions (and others?) off
viBREThe same enhanced regular expressions as ex
vim Read The User manual and The Reference manual