Regular Expression to Match a Line That Doesn't Contain a Word

A regular expression is a powerful tool for pattern matching and searching within text. It allows you to define a pattern and then search for matches within a given input string. In this article, we will explore how to use regular expressions to match lines that do not contain a specific word.

Understanding Regular Expressions

Before we dive into the solution, let's have a brief understanding of regular expressions. A regular expression is a sequence of characters that forms a search pattern. It can be used to match, split, and replace strings based on a defined pattern. Regular expressions are supported in many programming languages and text editors.

Using Regular Expressions to Match Lines that Do Not Contain a Word

Yes, it is possible to use a regular expression to match lines that do not contain a specific word. We can achieve this by using a negative lookahead assertion in our regex pattern.

A negative lookahead assertion allows us to specify a pattern that should NOT be present at a certain position in the input string.

(?!hede)

This negative lookahead assertion will ensure that the word "hede" is not present at the current position in the input string.

Example:

Let's consider the following input:

hoho
hihi
haha
hede

If we want to match lines that do not contain the word "hede", we can use the following regex pattern:

^(?!.*hede).*\n?

The above pattern will match the entire line if it does not contain the word "hede". Here's the breakdown of the pattern:

  • ^ - Start of the line
  • (?!.*hede) - Negative lookahead assertion to ensure "hede" is not present anywhere on the line
  • .* - Any character (except for a newline) repeated zero or more times
  • \n? - Optional newline character at the end of the line

Applying this pattern to the example input, our desired output of:

hoho
hihi
haha

will be matched.

Applying the Regex Pattern in Code

Now that we have the regex pattern, let's see how we can use it in different programming languages:

Python

import re

input_string = '''hoho
hihi
haha
hede'''

pattern = r'^(?!.*hede).*\n?'

output = re.findall(pattern, input_string, re.MULTILINE)
print(output)

JavaScript

const inputString = `hoho
hihi
haha
hede`;

const pattern = /^(?!.*hede).*\n?/g;

const output = inputString.match(pattern);
console.log(output);

Java

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexExample {
    public static void main(String[] args) {
        String inputString = "hoho\nhihi\nhaha\nhede";

        String pattern = "^(?!.*hede).*\\n?";

        Pattern regexPattern = Pattern.compile(pattern, Pattern.MULTILINE);
        Matcher matcher = regexPattern.matcher(inputString);

        while (matcher.find()) {
            System.out.println(matcher.group());
        }
    }
}

These examples demonstrate how to apply the regex pattern in Python, JavaScript, and Java. The output will be an array or list containing the matched lines without the word "hede".

By using regular expressions and negative lookahead assertions, we can easily match lines that do not contain a specific word. This approach provides a flexible and powerful solution for various text processing tasks.