Once you know regular expression syntax, you can match almost anything. But sometimes, it's a pain to think through how to get what you want. Table 32-4 lists some useful regular expressions that match various kinds of data you might have to deal with in the Unix environment. Some of these examples work in any program that uses regular expressions; others only work with a specific program such as egrep. (Section 32.20 lists the metacharacters that each program accepts.) The means to use a space as part of the regular expression. Bear in mind that you may also be able to use \< and \> to match on word boundaries.
Note that these regular expressions are only examples. They aren't meant to match (for instance) every occurrence of a city and state in any arbitrary text. But if you can picture what the expression does and why, that should help you write an expression that fits your text.
Item |
Example |
Regular expression |
---|---|---|
U.S. state abbreviation |
(NM) |
[A-Z][A-Z] |
U.S. city, state |
(Portland, OR) |
^.*,[A-Z][A-Z] |
Month day, year |
(JAN 05, 1993); (January 5, 1993) |
[A-Z][A-Za-z]\{2,8\}[0-9]\{1,2\},[0-9]\{4\} |
U.S. Social Security number |
(123-45-6789) |
[0-9]\{3\}-[0-9]\{2\}-[0-9]\{4\}= |
U.S. telephone number |
(547-5800) |
[0-9]\{3\}-[0-9]\{4\} |
Unformatted dollar amounts |
($1); ($ 1000000.00) |
\$*[0-9]+(\.[0-9][0-9])? |
HTML/SGML/XML tags |
(<h2>); (<UL COMPACT>) |
<[^>]*> |
troff macro with first argument |
(.SH "SEE ALSO") |
^\.[A-Z12]."[^"]*" |
troff macro with all arguments |
(.Ah "Tips for" "ex & vi") |
^\.[A-Z12].".*" |
Blank lines |
^$ |
|
Entire line |
^.*$ |
|
One or more spaces |
* |
--DD and JP
Copyright © 2003 O'Reilly & Associates. All rights reserved.