More about regular expressions



Last revision August 2, 2004

Table of Contents:
  1. Editor choices on Unix
  2. Characteristics, advantages, and disadvantages of vi
  3. Basic text editing operations in vi
  4. Regular expressions
  5. File searching with grep
  6. More about regular expressions
  7. Intermediate text editing with vi
  8. Vi Quick Reference

Search and substitution patterns in the vi editor use the same regular expression syntax, as do other text processing utilities in Unix. Some commands have extensions or additional metacharacters beyond this basic set.

Pangea, and other Unix systems based primarily on Berkeley Unix, do not permit repeated pattern match specifications of the form {n,m} or {m}, as described in McGilton.

What about logical combinations of regular expressions? There is no standard set of logical operators used by all utilities.

Logical "and" is relatively straightforward. You can use an expression that links the two desired strings with .* (match any characters in between). This only matches in the order given; not a true logical "and". Or, pipe two grep commands together where each matches one expression. Examples:

grep 'use.*this' filename

finds lines in filename that contain use followed by this, separated by zero or more characters.

grep 'use' filename | grep 'this'

finds all lines in filename that contain the string use, and then further restricts that to the subset that also contains this. Here, it does not matter whether "this" comes before or after "use" on the line.

egrep allows the | character as a logical "or" operator. Use to separate two regular expressions; it matches the line if either expression matches the line. For clarity about what is to be or'd, put the alternative regular expressions in sets of parentheses. To prevent the shell from thinking that those parentheses, vertical line, and other metacharacters should be interpreted by it (as process control and pipe symbols), enclose the entire expression in single quotes (').

Comments or Questions?