2013-03-10 55 views
0

有没有办法从“many”|“'正则表达式“匹配。如何从perl中的多个模式匹配得到许多匹配的模式

这里是我的代码,

#! /usr/bin/perl 
@matches = qw(google intel hp qualcomm app); 

$keyword = join('|', @matches); 

$string = "hello google app"; 

@founded = ($string =~ /($keyword)/); 

print "Founded keyword is:" . join(" ", @founded); 

我希望能得到“谷歌和应用程序”,因为这个关键字都在字符串匹配。但是,如何悲伤,只是让“谷歌”

回答

2

只需添加一个/g modifier到你的对手:

@found = ($string =~ /($keyword)/g); 

你会得到所有的比赛的方式。

+0

明白了。 Thx这么多 – wbo4958 2013-03-10 09:17:09

2

我认为你正在寻找两个列表的交集:

use Array::Utils qw(:all); 

my @matches = qw(google intel hp qualcomm app); 
my @find = qw(hello google app); 

my @result = intersect(@matches, @find); 
print "Founded keyword(s): ", join(" ", @result) . "\n"; 

该解决方案采用了Array::Utils模块

+0

它也可以,thx – wbo4958 2013-03-10 09:19:25