The following code:
Exits immediately and produces the message below when search pattern "\b\w+{3}\b" is entered:
#!/usr/bin/perl -w
#matchtest1.pl
use strict;
my($pattern);
my($true);
$_ = '1: A silly sentence (495,a) *BUT* one which will be useful. (3)';
do {
print "Enter a regular expression: ";
chomp($pattern = <STDIN>);
if (/($pattern)/g){ # Search pattern must be enclosed in ()'s
print "$pattern found in $_\n";
print "\$& = $&\n";
print "\$1 is '$1'\n" if defined $1;
print "\$2 is '$2'\n" if defined $2;
print "\$3 is '$3'\n" if defined $3;
print "\$4 is '$4'\n" if defined $4;
print "\$5 is '$5'\n" if defined $5;
}
else{
print "$pattern NOT found\n";
}
print "Enter 1 to continue, 0 to exit -> "; chomp($true=<STDIN>);
}while($true)
(Yes there is a nested quantifier in the search pattern "\b\w+{3}\b", but why does this cause the code to abort?)
Nested quantifiers in regex; marked by <-- HERE in m/(\b\w+{ <-- HERE 3}\b)/ at matchtest1.pl line 13, <STDIN> line 1.
Bonus Question!!!
Although the global modifier is used (/($pattern)/g) patterns with multiple matches only return the first match $1. Why?