本文介绍了如何在 Perl RegEx 中替换多个任意字符(包括换行符)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是MyTextString.txt的简化说明:

注意:BlaBla 代表任何字符,包括换行符.

Note: BlaBla stands for any character including new line character.

START BlaBla-In END BlaBla-Out-Between START BlaBla-In END BlaBla-Out-START BlaBla-In END BlaBla-Out-Between START BlaBla-In END 之间 ...

我正在寻找删除 END 和 START 之间的文本(BlaBla-Out-Between) 以得到如下结果:

I'm looking for removing text between END and START (BlaBla-Out-Between) to result like this:

START BlaBla-In END 换行符 START BlaBla-In END 换行符 START> BlaBla-In END 换行符 START BlaBla-In END ...

我有一个 perl 文件 changes.pl:

I've a perl file changes.pl:

BEGIN {
    @ARGV = map glob(""$_""), @ARGV;
}

s/(END).*?(START)/$1
$2/sg; #TEST

我应该使用这个CMD 行来执行我的替换:

I should execute my replaces using this CMD line:

perl -i.bak -p changes.pl My/File/Directory/MyTextString.txt

注意:changes.plCMD 行 运行良好,如本问题 与其他 RegEx 查找和替换字符串.

Note: the changes.pl and CMD line are working well as described in this question with other RegEx find and replace strings.

但是有了这个 RegEx 字符串,MyTextString.txt 不会发生任何修改:

But with this RegEx string no modifications happen to MyTextString.txt:

s/(END).*?(START)/$1
$2/sg;

我认为关于我的正则表达式语法的每一件事都没有问题,因为它在 regex 101 测试仪上运行良好.

I think every thing regarding my regular expression syntax is OK as it's working well on regex 101 tester.

我正在寻找使用提到的 changes.plCMD 行 来匹配和替换任何字符(包括换行符).简单地说,我正在寻找用 newline 替换 BlaBla-Out-Between.

I'm looking for matching and replacing any character (including newline) using mentioned changes.pl and CMD line. Simply, I'm looking for replacing BlaBla-Out-Between with newline.

推荐答案

在进行替换之前,您需要将整个文件变成一个字符串.-p 命令行开关一次只读取一行.

You need to slurp the whole file into a string before doing the substitution. The -p command line switch only reads a line at a time.

这意味着替换 s/(END).*?(START)/$1$2/sg 只会在有 END 的情况下删除任何内容code> 模式后跟 START 模式在同一行上.

It means that the substitution s/(END).*?(START)/$1$2/sg will only delete anything in those cases where there is an END pattern followed by a START pattern on the same single line.

要吞食文件,您可以指定八进制的输入记录分隔符 0777:

To slurp the file you can specify an input record separator of octal 0777:

perl -0777 -p -i.bak changes.pl MyTextString.txt

来自 perlrun:

-0[八进制/十六进制]

将输入记录分隔符 ($/) 指定为八进制或十六进制数.如果没有数字,则为空字符是分隔符.其他开关可能在数字.... 特殊值 00 将导致 Perl 在段落模式下吞咽文件.任何值 0400 或上面会导致 Perl 吞食整个文件,但按照惯例值 0777 是通常用于此目的的值.

specifies the input record separator ($/ ) as an octal or hexadecimal number. If there are no digits, the null character is the separator. Other switches may precede or follow the digits. ... The special value 00 will cause Perl to slurp files in paragraph mode. Any value 0400 or above will cause Perl to slurp files whole, but by convention the value 0777 is the one normally used for this purpose.

这篇关于如何在 Perl RegEx 中替换多个任意字符(包括换行符)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 22:00