^(?:(?!error).)*$
Regex options: Case insensitive, ^ and $ match at line breaks (“dot matches line breaks” must not be set) |
Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby |
In order to match a line that does not contain something, use negative lookahead
(described in Recipe 2.16). Notice that in
this regular expression, a negative lookahead and a dot are repeated
together using a noncapturing group. This is necessary to ensure that
the regex ‹error
›
fails at every position in the line. The ‹^
› and ‹$
› anchors at the edges of the regular expression
make sure you match a complete line, and additionally prevent the group
containing the negative lookahead from limiting it’s tests to only some
part of the line.
The options you apply to this regular expression determine whether it tries to match the entire subject string or just one line at a time. With the option to let ^ and $ match at line breaks enabled and the option to let dot match line breaks disabled, this regular expression works as described and matches line by line. If you invert the state of these two options, the regular expression will match any complete string that does not contain the word “error.”
Testing a negative lookahead against every position in a line or string is rather inefficient. This solution is intended to be used in situations where one regular expression is all that can be used, such as when using an application that can’t be programmed. When programming, it is more efficient to search through text line by line. Recipe 3.21 shows the code for this.
Recipe 5.10 shows how to match complete lines that do contain a particular word.
Recipe 3.21 includes code listings for searching through text line by line, which can simplify the process of searching within and identifying lines of interest.
Techniques used in the regular expressions in this recipe are discussed in Chapter 2. Recipe 2.4 explains that the dot matches any character. Recipe 2.5 explains anchors. Recipe 2.6 explains word boundaries. Recipe 2.9 explains grouping. Recipe 2.12 explains repetition. Recipe 2.16 explains lookaround.