Advanced Regular Expression Patterns
Lookahead Guard
A lookahead can require nearby text without consuming it as part of the match.
lookahead
Lookahead is useful when the program needs to confirm what comes next while keeping the matched text focused on the part being classified.
Lookahead Guard
lookahead_guard.pl
Replay: real traced execution (multi-file project)
use strict;
use warnings;
my $file = "report.csv";
my ($stem) = $file =~ /^([a-z]+)(?=\.csv$)/;
$stem = defined $stem ? $stem : "none";
my $csv = $stem ne "none" ? "yes" : "no";
print "file=$file\n";
print "stem=$stem\n";
print "csv=$csv\n";
use strict;
use warnings;
my $file = "image.png";
my ($stem) = $file =~ /^([a-z]+)(?=\.csv$)/;
$stem = defined $stem ? $stem : "none";
my $csv = $stem ne "none" ? "yes" : "no";
print "file=$file\n";
print "stem=$stem\n";
print "csv=$csv\n";
use strict;
use warnings;
my $file = "archive.csv.bak";
my ($stem) = $file =~ /^([a-z]+)(?=\.csv$)/;
$stem = defined $stem ? $stem : "none";
my $csv = $stem ne "none" ? "yes" : "no";
print "file=$file\n";
print "stem=$stem\n";
print "csv=$csv\n";
$file ← report.csv, $stem ← report, $csv ← yes
4my $file→ report.csv = "report.csv"; #@file="image.png", "archive.csv.bak"5my ($stem→ report) = $filereport.csv =~ /^([a-z]+)(?=\.csv$)/;6$stemreport = defined $stem ? $stem : "none";7my $csv→ yes = $stemreport ne "none" ? "yes" : "no";89print "file=$filereport.csv\n";10print "stem=$stemreport\n";11print "csv=$csvyes\n";outputfile=report.csv stem=report csv=yes
$file ← image.png, $stem ← (empty), $csv ← no
4my $file→ image.png = "image.png";5my ($stem→ (empty)) = $fileimage.png =~ /^([a-z]+)(?=\.csv$)/;6$stem→ none = defined $stem ? $stem : "none";7my $csv→ no = $stemnone ne "none" ? "yes" : "no";89print "file=$fileimage.png\n";10print "stem=$stemnone\n";11print "csv=$csvno\n";outputfile=image.png stem=none csv=no
$file ← archive.csv.bak, $stem ← (empty), $csv ← no
4my $file→ archive.csv.bak = "archive.csv.bak";5my ($stem→ (empty)) = $filearchive.csv.bak =~ /^([a-z]+)(?=\.csv$)/;6$stem→ none = defined $stem ? $stem : "none";7my $csv→ no = $stemnone ne "none" ? "yes" : "no";89print "file=$filearchive.csv.bak\n";10print "stem=$stemnone\n";11print "csv=$csvno\n";outputfile=archive.csv.bak stem=none csv=no