Skip to Content
Exploring Expect
book

Exploring Expect

by Don Libes
December 1994
Intermediate to advanced
606 pages
16h 7m
English
O'Reilly Media, Inc.
Content preview from Exploring Expect

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

AI Agents in Action

AI Agents in Action

Micheal Lanham
Learning Go

Learning Go

Jon Bodner

Publisher Resources

ISBN: 9781565920903Supplemental ContentErrata Page