4.4.1 Regular expressions
Regular expressions are used in many text processing tools. They are analogous
to the shell wildcards (see
Shell wildcards, Section
4.3.8), but they are both more complicated and more powerful.
The regular expression describes the matching pattern and is made up of text
characters and metacharacters. The metacharacter is just a
character with a special meaning. There are 2 major styles, BRE and ERE,
depending on the text tools as described in
Unix-like
text processing, Section 4.4.
For the EREs, the metacharacters include "\ . [ ]
^ $ * + ? ( ) { } |". The regular expression means:
-
c
-
\c
-
.
-
^
-
$
-
\<
-
\>
-
[abc...]
-
[^abc...]
-
r*
-
r+
-
r?
-
r1|r2
-
(r1|r2)
In BREs the metacharacters "+ ? ( ) { }
|" lose their special meaning; instead use the backslashed versions
"\+ \? \( \) \{ \} \|". Thus the grouping construct
(r1|r2) needs to be quoted as \(r1|r2\) in BREs.
Since emacs
, although being basically BRE, treats "+
?" as the metacharacters. Thus there are no needs
to quote them. See
Replacement expressions, Section
4.4.2 for how the grouping construct is used.
For example, grep
can be used to perform the text search using the
regular expression:
$ egrep 'GNU.*LICENSE|Yoyodyne' /usr/share/common-licenses/GPL
GNU GENERAL PUBLIC LICENSE
GNU GENERAL PUBLIC LICENSE
Yoyodyne, Inc., hereby disclaims all copyright interest in the program