The Politics Of Patterns

Creating a pattern that matches a string is not always an easy task. A common dilemma is whether to use a very conservative pattern or a more liberal pattern.

Conservative patterns typically have few or no wildcards and only match a limited number of strings. While easy to read, they carry the potential risk of not being able to match a string that deviates from the expected.

Liberal patterns are more forgiving with the ability to match any string that could conceivably appear. These patterns underspecify the requirements of a string and therefore risk of being able to match strings that were not intended to be matched.

For example, automating a login requires that the initial prompt be accepted. There is surprising nonconformity even at this level. For instance, UNIX systems commonly prompt with "login:" while VMS systems prompt with "Username:“. One way to automate this might be:

expect -re "(login|Username): "

But if you run into a system someday that just prompts "User“, it will not be accepted. This string and others can be added to the command, but eventually you may end up just accepting anything that ends with a colon and a space:

expect -re ".*: $"

The $ lowers the risk of the string appearing in the middle of some other output.

Incidentally, handling VMS and UNIX systems in a single script may seem hard to believe. However, the passmass script that comes with Expect as an example does just this. passmass sets your password on any number of hosts. The ...

Get Exploring Expect now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.