Regular Expressions Tables

Reference: Sarwar et al, Unix the textbook, 2nd Edition, Addison-Wesley, 2005, Pp. 248-250

Table 1: Regular Expression Operators and Their Support By Unix Tools

Name/Function Operator Example Usage Meaning Supported By
Alternation
|
x|y|z
x or y or z awk, egrep
Any Character
.
.com
Acom, Bcom, Ccom,
acom, bcom, ccom, ...
All
Beginning of Line
^
^x
A line that starts with x
as the first character
All
Concatenation
xyz
x followed by y followed by z All
End of Line
$
x$
A line that ends with x
as the last character
All
Escape Sequence
Cancels The special meaning
of the next character
following it
\
\*
An ordinary asterisk * ed, sed, vi, awk, grep
Delimiter: Marks the beginning
or End of a Regular Expression
/
/L..e/
Love, Lave, Live, Lose, ... ed, sed, vi, awk
Grouping
( ), \( \)
(xy)+
xy, xyxy, xyxyxy, ... All
Optional
?
xy?
0 or exactly 1 of previous character: x, xy Awk, egrep
Word Anchor
\< and \>
\<word\>
word (not part of a larger substring) All
Repetition
*
xy*
match 0 or more of previous character: x, xy, xyy, xyyy, ... All
Repetition
+
xy+
match 1 or more of previous character: xy, xyy, xyyy, ... Awk, egrep
Repetition
\{m,n\}
xy\{6,8\}
match at least 6 and no more than 8 of
previous character: xyyyyyy, xyyyyyyy, xyyyyyyyy
All
Set
menu of what can match
a single character position
[ ]
[Hh]ello
Hello, hello All
Set
menu of what cannot match
a single character position
[^ ]
[^A-IK-Z]ello
Jello, hello, bello, ... All

Table 2: Examples Of Regular Expressions for vi And Their Meaning

Regular Expression Meaning Examples
/^Yes/ The string Yes At
The Beginning Of The Line
Yes...
Yesteryear ...
Yesterday ...
/th/ The string th
Anywhere In The Line
the
there
path
bathing
/:$/ A Line Ending With A Colon ... the following:
... below:
... list of:
/[0-9]/ A Single Digit 0 1 2 3 4 5 6 7 8 9
/[a-z] [0-9]/ A Single Lower Case Letter
Followed By A Single Digit
a0 b1 c2 d3 e4 f5 w6 x7 y8 z9
/\.c/ An Ordinary Period Character
Followed By A 'c'
lab1.c program1.c client.c server.c
/[a-zA-Z ]*/ Any String Containing Zero Or More Of:
Upper And/Or Lower Case Letters
And/Or Spaces Only
Any String Without Numbers Nor
Punctuation: e.g. Not 767-!

Table 3: Some Commonly Used vi Commands Which Use Regular Expressions

Command Meaning
/ [0-9] / Do A Forward Search For A Single
Standalone Digit Character (Surrounded By Spaces)
In The Current File; Digits That Are In Substrings
Don't Match
?\.c[1-7] ? Do A Backward Search For Words Or
Strings In Words That End In .c Followed By
A Single Digit Between 1 And 7 Inclusive
:1,$s/:$/./ Search The Whole File And Substitute
A Period Whenever A Colon At The End Of The Line Is Found
:.,$s/^[Hh]ello /Greetings / Search From The Current Line To the End Of The File
For Hello Or hello At The Beginning Of The Line And Replace Either
With Greetings
:1,$s/^ *// Search The Entire File For Any Leading Spaces At The
Beginning Of All Lines And Delete Them