问题描述
在文本文件中进行正则表达式查找和替换时,我想跳过&忽略文本的某些部分。也就是说,应将文本的某些部分排除在搜索范围之外,而仅在搜索&更换其余零件。条件是:
While doing regex find-and-replace in text file, I wanna jump over & ignore certain segments of the text. That is, certain parts of the text should be excluded from the search, and only do search & replace in the remaining parts. The criteria is:
(1)在 START
和 END $ c之间的任何值$ c>应该从搜索&中排除。更换。
START
可能不在行首;
END
可能不在行尾;
一对 START
& END
可能跨越多行;
(1) anything between START
and END
should be excluded from the search & replace. START
may or may not be at the start of a line; END
may or may not be at the end of a line; one pair of START
& END
may span multiple lines;
(2)带有内联注释的任何内容 //
应该被忽略;
//
可能不在行首;
(2) anything wihtin inline comment //
should be ignored; //
may or may not be at the start of line;
(3)第一个之后的单词。
应该被忽略;
。
可能不在行首;
这个词可能紧跟在之后。
或用空格,换行符,制表符将它们分隔。
(3) the first word after .
should be ignored; .
may or may not be at the start of a line; the word may immediately follow .
or with spaces, newlines, tabs splitting them.
示例代码:
#!/usr/bin/env perl
use strict;
use warnings;
$/ = undef;
#iterate the DATA filehandle
while (<DATA>) {
# This one replaces ALL occurrences of pattern.
s/old/new/gs;
# How do I skip the unwanted segments and do the replace?
#print all
print;
}
##inlined data filehandle for testing.
__DATA__
xx START xx old xx END xx --> ignore
xx old xx --> REPLACE !
START xx old --> ignore
xx old xx END --> ignore
xx old xx --> REPLACE !
// xx old --> ignore
xx // xx old --> ignore
xx . old old xx --> ignore first one, replace second one
.
old --> ignore
(old) xx --> REPLACE !
xx old xx --> REPLACE !
预期输出为:
xx START xx old xx END xx --> ignore
xx new xx --> REPLACE !
START xx old --> ignore
xx old xx END --> ignore
xx new xx --> REPLACE !
// xx old --> ignore
xx // xx old --> ignore
xx . old new xx --> ignore first one, replace second one
.
old --> ignore
(new) xx --> REPLACE !
xx new xx --> REPLACE !
有人可以在这里使用正则表达式帮助我吗?几个小时前,我发布了一个类似的问题,但该帖子含糊不清,因此无法给出明确的答案。希望这篇文章可能是一个好& 清除问题。
Can anyone help me with the regex here? I posted a similar question couple of hours ago, but that post was full of ambiguities and precludes a clear answer. Hopefully this post may be a "good" & "clear" question.
推荐答案
您可以使用可以跳过某些内容。
You can use (*SKIP)(*F)
verbs to skip something.
(?:(?s:START.*?END)|\/\/.*|\.\s*\w+\b)(*SKIP)(*F)|old
这:(?:第1部分跳过|第2部分跳过| ...)(* SKIP)(* F)
|
部分要匹配
-
(? :
打开进行替换(?s:
和s
以使点匹配换行符 -
\w
匹配[A-Za-z0-9 _]
-
\b
匹配
(?:
opens a non capture group for alternation(?s:
withs
flag to make dot match newline\w
matches a word character[A-Za-z0-9_]
\b
matches a word boundary
这篇关于如何忽略部分文本,并在其余部分中进行搜索和替换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!