I am confused about the regular expression below. Please help me to understand it.
my $test = "fred andor berry";
if ($test =~ /fred (and|or) berry/) {
print "Matched!\n";
} else {
print "Did not match!\n";
}
I thought it would match, but I get “Did not match!”. If I add + in it, like this,
my $test = "fred andor berry";
if ($test =~ /fred (and|or)+ berry/) {
print "Matched!\n";
} else {
print "Did not match!\n";
}
Then it matches. I thought I can use and|or to match an expression with “and”, “or” and “andor”. No?
The expression
(and|or)will matchandoror, but notandor. When you add the+, it will accept two (actually one or more) consecutive matches of the same pattern, which allows it to matchandor. (First it matchesand, thenor.)