Regex 如何在perl中对数组应用负正则表达式?
具有以下特征: foo.pl:Regex 如何在perl中对数组应用负正则表达式?,regex,perl,regex-negation,Regex,Perl,Regex Negation,具有以下特征: foo.pl: #!/usr/bin/perl -w @heds = map { /_h.+/ and s/^(.+)_.+/$1/ and "$_.hpp" } @ARGV; @fls = map { !/_h.+/ and "$_.cpp" } @ARGV; print "heds: @heds\nfls: @fls"; 我想将标题与源文件分开,并且在输入时: $./foo.pl a b c_hpp d_hpp
#!/usr/bin/perl -w
@heds = map { /_h.+/ and s/^(.+)_.+/$1/ and "$_.hpp" } @ARGV;
@fls = map { !/_h.+/ and "$_.cpp" } @ARGV;
print "heds: @heds\nfls: @fls";
我想将标题与源文件分开,并且在输入时:
$./foo.pl a b c_hpp d_hpp
heds: e.hpp f.hpp
fls: e.cpp f.cpp a.cpp b.cpp
标题正确分开,但文件全部被占用。为什么?我已经应用了负正则表达式/_h、 +/
在映射中,因此不应考虑带有*\u h*
的文件,但它们是。为什么会这样?如何修复它
即使这样也不起作用:
@fls = map { if(!/_h.+/){ "$_.cpp" } } @ARGV;
尽管存在条件,
@heds
的映射{}
在$1
参数上包含一个替换,并对其进行更改。只需重新排序映射,以避免对@fls
产生影响,即可获得所需的结果。但是,如果您需要在这些映射之后访问@ARGV
,则它不再是原始的@ARGV
,如您的示例代码中所示
#!/usr/bin/perl -w
@fls = map { !/_h.+/ and "$_.cpp" } @ARGV;
@heds = map { /_h.+/ and s/^(.+)_.+/$1/ and "$_.hpp" } @ARGV;
print "heds: @heds\nfls: @fls\n";
请更正您的输出(e和f未通过),并显示输出的外观。但为什么map会更改原始ARGV?它应该只更改赋值数组(fls),而不是原始数组,它只从中读取数据。我不确定,但是$1也是中的第一个参数@ARGV@milanHrabos阅读它告诉您,修改映射体中的
$\uuU
会修改相应的列表元素。@Shawn好的,但是如果我想进行多次替换(从而修改$\uU
),那么每次修改都必须使用复制内容的临时数组?我可以从原始数组对数组进行多次修改而不更改其内容吗?如果不更改$\uz
,可以使用@heds=map{/\uh.+/和s/^(+.+)\uz+/$1/r..hpp“}@ARGV代码>。/r
修饰符使s///r返回已更改的字符串,而不更改原始匹配字符串(此处为$)。也许应该有一个我的?还有要过滤的grep?比如my@heds=map{s/^(+.+){/$1/r..hpp}grep{/_h.+/}@ARGV代码>