读书人

perl怎么输出多个匹配

发布时间: 2012-02-22 19:36:55 作者: rapoo

perl如何输出多个匹配?
比如:
#perl

my $text = "This is a pattern test. I want to test how to output two matched patterns in one line";

if($text=~/pattern/)
{
print "$&\n";
}

只能输出一个pattern,怎么才能输出2个?

[解决办法]
my $text = "This is a pattern test. I want to test how to output two matched patterns in one line";

@all_text=$text=~/pattern/g;

foreach(@all_text)
{
print $_."\n";
}

读书人网 >perl python

热点推荐