- 论坛徽章:
- 0
|
my $data = "Barney Rubble Fred Flintstone Wilma Flintstone";
my %last_name = ($data =~ /(\w+)\s+(\w+)/g);
foreach (sort keys %last_name){
printf "%s\t%s\n",$_,$last_name{$_};
}
#Output:
#Barney Rubble
#Fred Flintstone
#Wilma Flintstone
为什么不是
Barney Rubble
Rubble Fred
Fred Flintstone
Flintstone Wilma
Wilma Flintstone
哪位能解释一下么?给个连接,或相关文档也可以。
另外,用hash table 可以保存2个(\w+)的值;如果现在换成 $data =~ /(\w+)\s+(\w+)\s(\w+)/g的话,怎么把所有合适的匹配保存下来?
我希望能输出
Barney Rubble Fred
Flintstone Wilma Flintstone
===================
/(\w+)\s+(\w+)/g # 任何两个相邻 word,中间用 space隔开
每次匹配,保存了2个单元的值,所以可以用hash table直接保存
/(\w+)\s+(\w+)\s(\w+)/g # 任何三个相邻 word,中间用 space隔开
每次匹配,保存了3个单元的值 $1,$2,$3;我希望能够输出所以匹配的3个单元,比如
Barney Rubble Fred
Flintstone Wilma Flintstone
如果可以的话,希望能够给一下demo代码
谢谢 |
|