Chapter 6. Patterns, Actions, And Limits

In this chapter, I will describe the limits of patterns, including what to do when you hit them. I will also cover the darker side of range patterns—matching anything but certain characters. In the second half of the chapter, I will go into more detail on pattern actions including flow control. Finally, I will cover some miscellaneous pattern matching issues that do not fit anywhere else.

Matching Anything But

As I said in Chapter 5 (p. 113), pattern pieces match as many characters as possible. This makes it a little tricky to match a single line, single word, or single anything. For example, the regular expression ".*\n" matches a single line, but it also matches two lines because two lines end with a "\n“. Similarly, it matches three lines, four lines, and so on. If you want to read lines one at a time from another program, then you cannot use this kind of pattern. The solution is to use the "^“.

In Chapter 3 (p. 73), I showed that the "^" matches the beginning of the input buffer. When ^ is the first character of a regular-expression range, it means match anything but the given characters. For example, the regular expression [^ab] matches any character except a or b. The pattern [^a-zA-Z] matches any character but a letter.[26]

A range can be used to build larger patterns. The pattern "[^ ]*" matches the longest string not including a blank. For example, if the input buffer contained "For example, if the input buffer contained“, the following ...

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.