如何在Perl RegEx中替换多个任意字符(包括换行符)? [英] How to replace multiple any-character (including newline) in Perl RegEx?
问题描述
这是 MyTextString.txt 的简化说明:
注意: BlaBla 代表任何字符,包括换行符。
START BlaBla-In END BlaBla-Out-Between START BlaBla- strong> START BlaBla-In END BlaBla-In END b $ b
我正在寻找在END和START(BlaBla-Out-Between)之间移除文本,结果如下: p>
START BlaBla-In END 换行在 END 换行符 START BlaBla-In END 换行符开始 ..
我有一个perl文件 changes.pl :
BEGIN {
@ARGV = map glob(\$ _ \),@ARGV;
}
s /(END)。*?(START)/ $ 1\\\
$ 2 / sg; #TEST
我应该使用 CMD行执行替换:
perl -i.bak -p changes.pl My / File / Directory / MyTextString.txt
注意: changes.pl 和 CMD行 问题
但是对于这个RegEx字符串,MyTextString.txt不会发生任何修改:
s /(END)。*?(START)/ $ 1 \\\
$ 2 / sg;
我认为关于我的正则表达式语法的每一件事情都是可以的,因为它在 regex 101测试人员。
我在寻找匹配和使用提及的 changes.pl 和 CMD行替换任何字符(包括换行符)。 。
在进行替换之前,您需要将整个文件清理为字符串。 -p
命令行开关一次只能读取一行。
这意味着替换 s /(END)。*?(START)/ $ 1 \\\
只会删除那些有
$ 2 / sg END
模式,然后在同一行上使用 START
模式。
指定八进制的输入记录分隔符 0777
:
perl -0777 - p -i.bak changes.pl MyTextString.txt
从 perlrun :
-0 [八进制/十六进制]
将输入记录分隔符($ /)指定为八进制或十六进制数。如果没有数字,null
字符是分隔符。其他开关可以在
数字之前或之后。 ...特殊值00将使Perl在段落模式下清除文件。上面的任何值0400或
将导致Perl整个删除文件,但按照惯例,
值0777是通常用于此目的。
This is a simplified description of MyTextString.txt:
Note: BlaBla stands for any character including new line character.
START BlaBla-In END BlaBla-Out-Between START BlaBla-In END BlaBla-Out-Between START BlaBla-In END BlaBla-Out-Between START BlaBla-In END ...
I'm looking for removing text between END and START (BlaBla-Out-Between) to result like this:
START BlaBla-In END newline START BlaBla-In END newline START BlaBla-In END newline START BlaBla-In END ...
I've a perl file changes.pl:
BEGIN {
@ARGV = map glob("\"$_\""), @ARGV;
}
s/(END).*?(START)/$1\n$2/sg; #TEST
I should execute my replaces using this CMD line:
perl -i.bak -p changes.pl My/File/Directory/MyTextString.txt
Note: the changes.pl and CMD line are working well as described in this question with other RegEx find and replace strings.
But with this RegEx string no modifications happen to MyTextString.txt:
s/(END).*?(START)/$1\n$2/sg;
I think every thing regarding my regular expression syntax is OK as it's working well on regex 101 tester.
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.
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.
It means that the substitution s/(END).*?(START)/$1\n$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.
To slurp the file you can specify an input record separator of octal 0777
:
perl -0777 -p -i.bak changes.pl MyTextString.txt
From perlrun:
-0[octal/hexadecimal]
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中替换多个任意字符(包括换行符)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!