sub test {
my ($sentence, $re) = @_;
print("Sentence: $sentence\n");
print("Regexp: $re\n");
if ($sentence =~ /($re)/) {
printf("Matched %d characters (%s) at pos %d\n",
length($1), $1, $-[1]);
} else {
print("No match\n");
}
print("\n");
}
test("1234", qr/\d*/); # Matched 4 characters (1234) at pos 0
test("abc1234", qr/\d*/); # Matched 0 characters () at pos 0
test("abc", qr/\d*/); # Matched 0 characters () at pos 0
test("1234", qr/\d+/); # Matched 4 characters (1234) at pos 0
test("abc1234", qr/\d+/); # Matched 4 characters (1234) at pos 3 (*1)
test("abc", qr/\d+/); # No match (*2)
*1 - Since it failed at positions 0, 1 and 2.
*2 - Since it failed at all positions.
Update: Combined the code and the output to condense the node and improve readability.
|