Skip to Content
Regular Expressions Cookbook, 2nd Edition
book

Regular Expressions Cookbook, 2nd Edition

by Jan Goyvaerts, Steven Levithan
August 2012
Intermediate to advanced
609 pages
19h 16m
English
O'Reilly Media, Inc.
Content preview from Regular Expressions Cookbook, 2nd Edition

5.11. Match Complete Lines That Do Not Contain a Word

Problem

You want to match complete lines that do not contain the word error.

Solution

^(?:(?!\berror\b).)*$
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

Discussion

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 \berror\b 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.”

Caution

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

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

Regular Expressions Cookbook

Regular Expressions Cookbook

Jan Goyvaerts, Steven Levithan

Publisher Resources

ISBN: 9781449327453Supplemental ContentErrata Page