正则表达式查找 &替换 Y 文本和 Z 文本之间的 X 文本 [英] RegEx to Find & Replace X text in between Y text and Z text

查看:63
本文介绍了正则表达式查找 &替换 Y 文本和 Z 文本之间的 X 文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

各位论坛成员,我正在使用 NotePad++ 6.1.2 版,我需要知道是否可以使通用表达式执行查找和操作.完成以下操作的替换操作:

Fellow Forum Members, I'm using NotePad++ version 6.1.2 and I need to know if it is possible to make a General Expression perform a Find & Replace operation that accomplishes the following:

它会找到位于Y"和Z"文本之间的X"文本并将其替换为空,以有效删除X"文本以及Y"和Z"文本.所以对于下面显示的句子,一般表达式需要删除单词Begin"和End"之间的所有文本以及单词Begin"和End"以及删除所有内容.

It finds "X" text located in between "Y" and "Z" text and replaces it with nothing to effectively delete both the "X" text and also the "Y" and "Z" text as well. So for the sentence shown below the general expression needs to delete all text between the words "Begin" and "End" and also the words "Begin" and "End" as well to delete everthing.

开始X"个文本结束

我应该指出开始"和结束"在整个文本文件中是一致的.因此,我需要通用表达式来查找开始"和结束"的每个实例,然后删除它们以及它们之间的任何文本.任何帮助将不胜感激.谢谢.

I should point out that "Begin" and "End" are consistant throughout the text file. Therefore, I need the general expression to find every instance of "Begin" and "End", followed by deleting them and also whatever text is in between. Any help will be appreciated. Thanks.

推荐答案

所以你想删除 Y, X, Z 当且仅当 X 介于 Y 和 Z 之间:

So you want to delete Y, X, Z if and only if X is between Y and Z:

一个例子:

Y = "BEGIN"
Z = "END"
X = "CHOUCROUTE"

模式:

search : BEGIN(?>[^CE]+|C(?!HOUCROUTE)|E(?!ND))*CHOUCROUTE[\s\S]*?END
replace: nothing

这部分(?>[^CE]+|C(?!HOUCROUTE)|E(?!ND))* 需要匹配除关键字或结束词之外的所有内容,让我们仔细看看它:

This part (?>[^CE]+|C(?!HOUCROUTE)|E(?!ND))* is needed to match all except the keyword or the closing word, lets look at it in detail:

(?>                 # open an atomic group
    [^CE]+          # all except the letters C and E
  |                 # OR
    C(?!HOUCROUTE)  # C not followed by the end of the keyword
  |                 # OR
    E(?!ND)         # E not followed by the end of the closing word
)*                  # repeat the group zero or more times

原子组的目标是避免灾难性的回溯.原子组禁止正则表达式引擎回溯.如果我改用非捕获组并且正则表达式引擎没有找到关键字,它会尝试所有可能的划分.

The goal of the atomic group is to avoid catastrophic backtracking. The atomic group forbids the regex engine to backtrack. If I had used a non-capturing group instead and if the regex engine had not found the keyword, it would have tried all possible divisions.

如果您使用没有原子组功能的旧版记事本++,您可以使用此技巧升级您的版本或模拟它(默认情况下,lookahead 的内容是原子的):

If you use an older version of notepad++ that doesn't have the atomic group feature, you can upgrade your version or emulate it using this trick (the content of a lookahead is atomic by default):

((?=([^CE]+|C(?!HOUCROUTE)|E(?!ND)))\1)* 

这篇关于正则表达式查找 &替换 Y 文本和 Z 文本之间的 X 文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆