Scalar and Text Basics
Regex Match
A match operator checks whether a scalar follows a small text pattern.
regex match
A regex match returns true when text fits the pattern.
Regex Match
regex_match.pl
Replay: real traced execution (multi-file project)
use strict;
use warnings;
my $code = "AB-123";
my $matched = $code =~ /^([A-Z]+)-(\d+)$/;
my $matched_text = $matched ? "yes" : "no";
my $prefix = $matched ? $1 : "none";
my $number = $matched ? $2 : "none";
print "code=$code\n";
print "matched=$matched_text\n";
print "prefix=$prefix\n";
print "number=$number\n";
use strict;
use warnings;
my $code = "CD-9";
my $matched = $code =~ /^([A-Z]+)-(\d+)$/;
my $matched_text = $matched ? "yes" : "no";
my $prefix = $matched ? $1 : "none";
my $number = $matched ? $2 : "none";
print "code=$code\n";
print "matched=$matched_text\n";
print "prefix=$prefix\n";
print "number=$number\n";
use strict;
use warnings;
my $code = "bad";
my $matched = $code =~ /^([A-Z]+)-(\d+)$/;
my $matched_text = $matched ? "yes" : "no";
my $prefix = $matched ? $1 : "none";
my $number = $matched ? $2 : "none";
print "code=$code\n";
print "matched=$matched_text\n";
print "prefix=$prefix\n";
print "number=$number\n";
$code ← AB-123, $matched ← 1, $matched_text ← yes, $prefix ← AB
4my $code→ AB-123 = "AB-123"; #@code="CD-9", "bad"5my $matched→ 1 = $codeAB-123 =~ /^([A-Z]+)-(\d+)$/;6my $matched_text→ yes = $matched1 ? "yes" : "no";7my $prefix→ AB = $matched1 ? $1 : "none";8my $number→ 123 = $matched1 ? $2 : "none";910print "code=$codeAB-123\n";11print "matched=$matched_textyes\n";12print "prefix=$prefixAB\n";13print "number=$number123\n";outputcode=AB-123 matched=yes prefix=AB number=123
$code ← CD-9, $matched ← 1, $matched_text ← yes, $prefix ← CD
4my $code→ CD-9 = "CD-9";5my $matched→ 1 = $codeCD-9 =~ /^([A-Z]+)-(\d+)$/;6my $matched_text→ yes = $matched1 ? "yes" : "no";7my $prefix→ CD = $matched1 ? $1 : "none";8my $number→ 9 = $matched1 ? $2 : "none";910print "code=$codeCD-9\n";11print "matched=$matched_textyes\n";12print "prefix=$prefixCD\n";13print "number=$number9\n";outputcode=CD-9 matched=yes prefix=CD number=9
$code ← bad, $matched ← (empty), $matched_text ← no, $prefix ← none
4my $code→ bad = "bad";5my $matched→ (empty) = $codebad =~ /^([A-Z]+)-(\d+)$/;6my $matched_text→ no = $matched(empty) ? "yes" : "no";7my $prefix→ none = $matched(empty) ? $1 : "none";8my $number→ none = $matched(empty) ? $2 : "none";910print "code=$codebad\n";11print "matched=$matched_textno\n";12print "prefix=$prefixnone\n";13print "number=$numbernone\n";outputcode=bad matched=no prefix=none number=none
Follow the Match
$codestarts asAB-123.- The pattern looks for letters, a dash, then digits.
AB-123matches.- The captures are prefix
ABand number123. - The program prints
matched=yes,prefix=AB, andnumber=123. | code | matched | prefix | number | | --- | --- | --- | --- | | AB-123 | yes | AB | 123 | | CD-9 | yes | CD | 9 | | bad | no | none | none |
Exercise: regex_match.pl
Reproduce prefix=AB and number=123, then use codes CD-9 and bad to predict the match result.