Total Hit Counter

Regular expressions


 Regular expressions

Regular expressions are used in many text processing tools. They are analogous to the shell globs, but they are more complicated and 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 above.
Table 1.24. Metacharacters for BRE and ERE
BREEREdescription of the regular expression
\ . [ ] ^ $ *\ . [ ] ^ $ *common metacharacters
\+ \? \( \) \{ \} \| BRE only "\" escaped metacharacters
 + ? ( ) { } |ERE only non-"\" escaped metacharacters
ccmatch non-metacharacter "c"
\c\cmatch a literal character "c" even if "c" is metacharacter by itself
..match any character including newline
^^position at the beginning of a string
$$position at the end of a string
\<\<position at the beginning of a word
\>\>position at the end of a word
\[abc…\][abc…]match any characters in "abc…"
\[^abc…\][^abc…]match any characters except in "abc…"
r*r*match zero or more regular expressions identified by "r"
r\+r+match one or more regular expressions identified by "r"
r\?r?match zero or one regular expressions identified by "r"
r1\|r2r1|r2match one of the regular expressions identified by "r1" or "r2"
\(r1\|r2\)(r1|r2)match one of the regular expressions identified by "r1" or "r2" and treat it as a bracketed regular expression

The regular expression of emacs is basically BRE but has been extended to treat "+"and "?" as the metacharacters as in ERE. Thus, there are no needs to escape them with "\" in the regular expression of emacs.
grep(1) can be used to perform the text search using the regular expression.
For example, try the following
$ 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

No comments: