How to use negative lookahead (?!...) in Perl regex?
Question
How to use negative lookahead (?!...) in Perl regex?
Negative lookahead (?!...) in Perl regular expressions is a powerful zero-width assertion that allows you to match a string only if it is not followed by a certain pattern. It’s used to exclude matches that are followed by a specific sequence, without consuming any characters in the overall match.
Understanding Negative Lookahead
A negative lookahead (?!pattern) asserts that what immediately follows the current position in the string does not match pattern. It is zero-width, meaning it doesn’t consume characters — it only checks ahead.
This is very useful when you want to match something conditionally, for example:
- Match
fooonly if it is NOT followed bybar. - Find all words not followed by a digit.
Basic Syntax
# Negative lookahead syntax
/(pattern1)(?!pattern2)/
This matches pattern1 only if it is not followed by pattern2.
Example: Match "foo" not followed by "bar"
#!/usr/bin/perl
use strict;
use warnings;
my @test_strings = (
"foobar",
"foobaz",
"foo123",
"foo",
);
for my $str (@test_strings) {
if ( $str =~ /foo(?!bar)/ ) {
print "'$str' matches 'foo' NOT followed by 'bar'\n";
} else {
print "'$str' does NOT match\n";
}
}
Output:
'foobar' does NOT match 'foobaz' matches 'foo' NOT followed by 'bar' 'foo123' matches 'foo' NOT followed by 'bar' 'foo' matches 'foo' NOT followed by 'bar'
In this example, foo(?!bar) matches foo only if the next characters are not bar. The string foobar is excluded because foo is directly followed by bar. Others match because the lookahead condition is satisfied.
Important Notes on Perl Lookaheads
- Zero-width assertion: Lookaheads don’t consume characters, so they do not become part of the matched string.
- Lookahead vs. Lookbehind: Negative lookaheads check what follows the current position; lookbehinds check what immediately precedes.
- Combining with other patterns: You can chain multiple lookaheads or combine negative and positive lookaheads for complex conditions, e.g.,
foo(?!bar)(?=baz). - Versions: Perl has supported lookaheads since very early versions, so no special version requirement is needed.
Common Pitfalls
- Using greedy quantifiers inside lookaheads can cause unexpected failures. Lookaheads do not consume input, so your match position stays the same.
- Misunderstanding zero-width nature: When replacing or capturing, remember lookaheads don’t consume characters, so your replacements won’t remove the lookahead pattern.
- Lookaheads do not backtrack into what they assert: If the lookahead fails, the whole match fails immediately.
Summary
Negative lookahead (?!pattern) is a concise, elegant way to exclude matches followed by certain substrings without consuming them. It’s best used for precise pattern matching conditions when you want to ensure not to match if a forbidden sequence occurs immediately after.
Verified Code
Executed in a sandbox to capture real output. • v5.34.1 • 5ms
(empty)(empty)Was this helpful?
Related Questions
- How to validate email addresses using Perl regex?
- How to split a string by regex pattern in Perl?
- How to use greedy vs non-greedy quantifiers in Perl regex?
- How to match start and end of string with ^ and $ in Perl?
- How to use alternation | in Perl regex?
- How to use the qr// operator to compile regex in Perl?