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. Here are 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. (Article 26.9 lists the metacharacters that each program accepts.) The means to use a space as part of the regular expression.
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.
US State Abbreviation | [A-Z][A-Z] |
(NM) | |
US City, State | ^.*,[A-Z][A-Z] |
(Portland, OR) | |
Month Day, Year | [A-Z][A-Za-z]\{2,8\}[0-9]\{1,2\},[0-9]\{4\} |
(JAN 05, 1993) | |
(January 5, 1993) | |
US Social Security Number | [0-9]\{3\}-[0-9]\{2\}-[0-9]\{4\} |
(123-45-6789) | |
US Telephone Number | [0-9]\{3\}-[0-9]\{4\} |
(547-5800) | |
UFormatted Dollar Amounts | \$*[0-9]+(\.[0-9][0-9])? |
($1) | |
($ 1000000.00) | |
troff In-line Font Requests | \\f[(BIRP]C*[BW]* |
(\fR) | |
(\f(CB) | |
troff Requests | ^\.[a-z][a-z] |
(.bp) | |
HTML tags | <[^>]*> |
(<h2>) | |
(<UL COMPACT>) | |
troff Macro with Arguments | ^\.[A-Z12].".*" |
(.Ah "Tips for" "ex & vi") | |
Blank Lines | ^$ |
Entire Line | ^.*$ |
One or More Spaces | * |
-
,