O'Reilly logo

Exploring Expect by Don Libes

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 5. Regular Expressions

The previous chapter described glob patterns. You were probably familiar with them from the shell. Glob patterns are very simple and are sufficient for many purposes. Hence they are the default style of pattern matching in expect commands. However, their simplicity brings with it limitations.

For example, glob patterns cannot match any character not in a list of characters, nor can glob patterns match a choice of several different strings. Both of these turn out to be fairly common tasks. And while both can be simulated with a sequence of several other commands, Expect provides a much more powerful and concise mechanism: regular expressions.

Regular Expressions—A Quick Start

In order to jumpstart your knowledge of regular expressions (regexp for short), I will start out by noting the similarities. As the following table of examples shows, every glob pattern is representable by a regular expression. In addition, some regular expressions cannot be represented as glob patterns.

Table 5-1. Comparison of glob patterns and regular expressions

glob

regexp

English

s

s

literal s

\*

\*

literal *

^

^

beginning of string

$

$

end of string

[a-z]

[a-z]

any character in the range a to z

 

[^a-z]

any character not in the range a to z

?

.

any single character

*

.*

any number of characters

For example, both the glob pattern foo and the regular expression foo match the literal string "foo“. Backslash works in the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required