【发布时间】:2016-03-15 18:53:21
【问题描述】:
我是 Perl 初学者。我通常使用“sed”进行模式替换,但是由于我需要在一个大文件中注释掉一些多行部分,所以我认为使用 Perl 在多行搜索中更强大。
模式示例:
word1 {
do not care1
do not care2
...
} word2
预期输出:
#word1 {
# do not care1
# do not care2
# ...
#} word2
我发现关于 perl 多行匹配的非常有用的问答,但我找不到任何好的/紧凑/简单的解决方案来为所有匹配的行(不仅仅是第一行)添加前缀。
现在,这是我写的代码:
perl -0777 -pe 's/(word1\s*{[^}]*\s*}\s*word2\s.*)/#$1/gm'
但不幸的是,它不能为所有匹配的行添加前缀(#),如下所示:
#word1 {
do not care1
do not care2
...
} word2
你能帮忙吗?我希望代码仍然在 1 行:)
【问题讨论】:
标签: regex perl sed multiline prefix