【发布时间】:2011-02-18 14:52:30
【问题描述】:
文件看起来像
[N the computer end] [M whatever] [N you look] [N why not]
我只需要括号中以[N开头的单词
所以这里我要拿到电脑端你看看为什么不
他们可能在同一行,也可能不在同一行
我尝试过这样的事情:
if($line =~/\[N(.+?)\]/)
但它只匹配每行的第一行。
【问题讨论】:
文件看起来像
[N the computer end] [M whatever] [N you look] [N why not]
我只需要括号中以[N开头的单词
所以这里我要拿到电脑端你看看为什么不
他们可能在同一行,也可能不在同一行
我尝试过这样的事情:
if($line =~/\[N(.+?)\]/)
但它只匹配每行的第一行。
【问题讨论】:
在正则表达式上使用g 修饰符来查找“g”全局匹配。像这样:
while ($line =~ /\[N(.+?)\]/g) {
# $1 contains the text between "[N" and "]"
}
或者像这样:
my @matches = $line =~ /\[N(.+?)\]/g;
# @matches contains all of the matching items of text
【讨论】:
您需要将其更改为 while 循环以遍历每个组匹配。 Perl documentation 说明了这一点。
【讨论】: