如何让perl在命令行中打印所有匹配项?

时间:2016-07-05 02:01:01

标签: regex perl

我想在命令行中打印所有匹配项,如下所示:

perl -0777 -nle 'print "$1\n" if /ModelProxy\("([\w*.]+)"\);/g' test.txt
像这样test.txt

var orderform = new ModelProxy("orderform.b2b.*");
var orderform2b = new ModelProxy("web.orderform_2b.*");
var o2oOrderform = new ModelProxy("orderform.o2o.*");
var logistics = new ModelProxy("supply.logistics.*");
var tyreurgent = new ModelProxy("o2o.tyreurgent.*");
var common = new ModelProxy("web.common.file.*");
var cartPipeData = load("pipe/cartPipeData");
var tsCartPipeData = load("pipe/tsCartPipeData").default;

我想要的是:

orderform.b2b.*
web.orderform_2b.*
orderform.o2o.*
supply.logistics.*
o2o.tyreurgent.*
web.common.file.*

但我得到了第一个:

orderform.b2b.*

1 个答案:

答案 0 :(得分:2)

if //g毫无意义。您正在寻找while //g

perl -0777ne'print "$1\n" while /ModelProxy\("([\w*.]+)"\);/g' test.txt

你期望在比赛中间换线吗?如果没有,则无需将整个文件读入内存。

perl -nle'print for /ModelProxy\("([\w*.]+)"\);/g' test.txt