Regular expression to match a line that doesn’t contain a word ?

To create a regular expression that matches a line not containing a specific word, use a negative lookahead assertion. This ensures the word does not appear anywhere in the line. Here’s how to do it:

Regex Pattern

^(?!.*\bword\b).*$

Explanation:

  • ^: Start of the line.
  • (?!.*\bword\b): Negative lookahead to ensure the line does not contain the word word (with word boundaries).
  • .*: Match any characters (except newlines) until the end of the line.
  • $: End of the line.

Examples

1. Basic Usage

^(?!.*error).*$
  • Matches lines without the word error.
  • Example Match: "Success: Operation completed."
  • No Match: "Error: Invalid input."

2. Case-Insensitive Match

Add the i flag to ignore case:

^(?!.*error).*$  # Case-sensitive (default)
^(?i)(?!.*error).*$  # Case-insensitive (syntax varies by regex engine)
  • Example Match: "WARNING: No issues found." (avoids ERROR, Error, etc.).

Key Modifications

1. Exclude Empty Lines

Use .+ instead of .* to require at least one character:

^(?!.*error).+$

2. Match Partial Words (No Word Boundaries)

Remove \b to match substrings:

^(?!.*error).*$
  • Example No Match: "debuggerror" (contains “error” as a substring).

3. Multiline Input

Use the m (multiline) flag to match across multiple lines:

/^(?!.*error).*$/gm  # In JavaScript

Code Implementation

JavaScript

const regex = /^(?!.*\berror\b).*$/gm;
const text = `
Success: Task completed.
Error: File not found.
Warning: Low memory.
`;

// Filter lines without "error"
const validLines = text.match(regex) || [];
console.log(validLines); // ["Success: Task completed.", "Warning: Low memory."]

Python

import re

text = """
Success: Task completed.
Error: File not found.
Warning: Low memory.
"""

pattern = re.compile(r'^(?!.*\berror\b).*$', re.MULTILINE)
valid_lines = [line.strip() for line in pattern.findall(text)]
print(valid_lines)  # ['Success: Task completed.', 'Warning: Low memory.']

Common Edge Cases

  1. Empty Lines:
  • ^(?!.*error).*$ matches empty lines. Use ^(?!.*error).+$ to exclude them.
  1. Special Characters:
  • Escape regex metacharacters (e.g., \bfoo\b becomes \bfoo\b if foo is literal).
  1. Multi-Line Inputs:
  • Use flags like m (multiline) and s (dotall) depending on your regex engine.

Why This Works

  • The negative lookahead (?!.*word) checks the entire line for the absence of word.
  • Word boundaries (\b) prevent partial matches (e.g., err in error).

Use this regex to filter logs, validate input, or parse files while excluding lines with specific keywords!

Leave a Reply

Your email address will not be published. Required fields are marked *