Perl m 函数

描述

This match operator is used to match any keyword in given expression. Parentheses after initial m can be any character and will be used to delimit the regular expression statement.

Regular expression variables include $, which contains whatever the last grouping match matched; $&, which contains the entire matched string; $`, which contains everything before the matched string; and $', which contains everything after the matched string.


语法

以下是此函数的简单语法 −

m//

返回值

This function returns 0 on failure and 1 on success,


示例

以下是显示其基本用法的示例代码 −

#!/usr/bin/perl -w

$string = "The food is in the salad bar";
$string =~ m/foo/;
print "Before: $`\n";
print "Matched: $&\n";
print "After: $'\n";

执行上述代码时,会产生以下结果 −

Before: The 
Matched: foo
After: d is in the salad bar

❮ Perl 函数参考