我之前曾在Groovy中询问过如何做到这一点.但是,由于所有CPAN库,现在我在Perl中重写我的应用程序.
如果页面包含以下链接:
<a href="http://www.google.com">Google</a> <a href="http://www.apple.com">Apple</a>
输出将是:
Google, http://www.google.com Apple, http://www.apple.com
在Perl中执行此操作的最佳方法是什么?
我试图在Perl中提出一个匹配多个模式的正则表达式,并像preg_match_allPHP 一样返回所有这些模式.
这就是我所拥有的:
$str = 'testdatastring';
if($str =~ /(test|data|string)/) {
print "its found index location: $0 $-[0]-$+[0]\n";
print "its found index location: $1 $-[1]-$+[1]\n";
print "its found index location: $2 $-[2]-$+[2]\n";
print "its found index location: $3 $-[3]-$+[3]\n";
}
Run Code Online (Sandbox Code Playgroud)
这只给了我第一场比赛,其中就是'测试'.我希望能够匹配所有出现的指定模式:'test','data'和'string'.
我知道在PHP中,你可以使用preg_match_all来达到这种目的:
if(preg_match_all('/(test|data|string)/', 'testdatastring', $m)) {
echo var_export($m, true);
}
Run Code Online (Sandbox Code Playgroud)
上面的PHP代码将匹配所有3个字符串:'test','data'和'string'.
我想知道如何在Perl中执行此操作.任何帮助将不胜感激.