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)