该文件看起来像
[N the computer end] [M whatever] [N you look] [N why not]
我只需要以[N
开头的括号中的字词
所以在这里,我想让计算机结束你看起来为什么不
他们可能会或可能不会在同一行
我试过这样的事情:
if($line =~/\[N(.+?)\]/)
但它只匹配每一行的第一行。
答案 0 :(得分:4)
使用正则表达式上的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
答案 1 :(得分:0)
您需要将其更改为while循环以迭代每个组匹配。 Perl documentation说明了这一点。