Negative Lookahead in Regular Expressions
1. What is Negative Lookahead?
Negative Lookahead is a type of lookaround in regular expressions that asserts that a pattern does not match after the current position. It is denoted by (?! ... )
. This construct is useful when you want to ensure that a certain pattern does not follow another pattern.
2. Syntax of Negative Lookahead
The syntax for Negative Lookahead is (?! ... )
, where the pattern inside the parentheses specifies what should not follow the current position. If the pattern inside the lookahead matches, the entire match fails.
Example:
Pattern: abc(?!def)
Matches: "abcxyz"
Explanation: The pattern matches "abc" only if it is not followed by "def".
3. Practical Use Cases
Negative Lookahead is particularly useful in scenarios where you need to exclude certain patterns from matching. For example, it can be used to validate email addresses, ensuring that the domain does not end with certain undesirable suffixes.
Example:
Pattern: \b\w+@(?!example\.com)\w+\.\w+\b
Matches: "user@domain.com"
Does not match: "user@example.com"
Explanation: The pattern matches email addresses that do not end with "example.com".
4. Combining Negative Lookahead with Other Patterns
Negative Lookahead can be combined with other patterns to create more complex and precise regular expressions. For example, you can use it to match strings that do not contain certain characters or sequences.
Example:
Pattern: a(?!b)c
Matches: "ac"
Does not match: "abc"
Explanation: The pattern matches "ac" only if "a" is not followed by "b".
5. Nested Negative Lookahead
Negative Lookahead can be nested within other lookaheads to create even more specific conditions. This allows for fine-grained control over the matching process.
Example:
Pattern: a(?!b(?!c))
Matches: "ac"
Does not match: "abc"
Explanation: The pattern matches "a" only if it is not followed by "b" that is not followed by "c".
6. Real-World Application
In real-world applications, Negative Lookahead is often used in validation scripts, data extraction, and text processing tasks. It helps in ensuring that the extracted data meets specific criteria without unwanted patterns.
Example:
Pattern: \b\d{3}(?!-)\d{2}\b
Matches: "12345"
Does not match: "123-45"
Explanation: The pattern matches five-digit numbers that do not contain a hyphen after the first three digits.