Perl命令行搜索并用多个表达式替换

blu*_*ale 16 perl

我使用Perl来搜索和替换多个正则表达式:当我执行以下命令时,我收到一个错误:

prompt> find "*.cpp" | xargs perl -i -pe 's/##(\W)/\1/g' -pe 's/(\W)##/\1/g'
syntax error at -e line 2, near "s/(\W)##/\1/g"
Execution of -e aborted due to compilation errors.
xargs: perl: exited with status 255; aborting
Run Code Online (Sandbox Code Playgroud)

多个-e在Perl中有效,那为什么这不起作用?这个问题有方法解决吗?

dpp*_*dpp 40

Several -e's are allowed.

You are missing the ';'

find "*.cpp" | xargs perl -i -pe 's/##(\W)/\1/g;' -pe 's/(\W)##/\1/g;'
Run Code Online (Sandbox Code Playgroud)

Perl statements has to end with ;. Final statement in a block doesn't need a terminating semicolon. So a single -e without ; will work, but you will have to add ; when you have multiple -e statements.

  • 或者`xargs perl -i -pe's/##(\ W)/\1/g; S /(\ W)## /\1 /克;'` (6认同)