perl的正则表达式怎么了?
直接上问题:
my $tmp = "LOG(\"\");";
$tmp =~ /LOG\("(.*)"/;
print "tmp is $tmp\n";
$tmp =~ s//hello/;
print "tmp is $tmp\n";
这段代码的输出结果是:
tmp is LOG("");
tmp is hello);
然后 我把上面的 第二行代码也删掉($tmp =~ /LOG\("(.*)"/;)
然后再输出就是 :
tmp is LOG("");
tmp is helloLOG("");
我的问题 就是 第二行 代码 啥都没做啊,为啥 输出结果会不一样呢?
再追问一个 问题:
my $tmp ="ab";
$tmp =~ s//m/g;
这个的输出为啥是mab;而不是无法执行?我觉得任意两个字符之间都是空字符啊,为啥只替换了开头?
[解决办法]
If the regexp evaluates to the empty string, the regexp in the last successful match is used instead. So we have
"dog" =~ /d/; # 'd' matches "dogbert =~ //; # this matches the 'd' regexp used before