正则表达式 sublimetext 替换文本文件中特定字符串后的循环字符串

Regex sublimetext replacement of a recurrent string in a text file after a specific string

在提问之前,我已经在这里搜索了整整两天 google 和 regex101。
这是我的文件(数千个)的样子:

FIRST LINE THAT MIGHT CONTAIN ON .
SECTION1
SOME TEXT THAT MIGHT CONTAIN ON .
SECTION2
ON 04/1/2017 SOME TEXT
ON 25/1/2017 SOME TEXT
ON 15/2/2017 SOME TEXT

我只需要删除 SECTION2 中出现的每 ON 次。
我显然无法 post 那些在两天的反复试验中不起作用的模式。 (它会用不相关的东西淹没搜索引擎,同时证明我的智慧有限 - 在这个主题上 ;-)

你可以用这个模式来做:

(?:\G(?!\A)|\A(?>.*\R)*?SECTION2\h*\R)(?>.*\R)*?\KON\h

demo

我们的想法是构建一个模式,使用 \G 锚点只能 returns 连续匹配。此锚点在字符串开头或匹配成功后的位置成功。

图案详情:

(?: # non-capturing group: two possible starts
    \G(?!\A)  # the position after a previous match
  |           # OR
    \A(?>.*\R)*?SECTION2\h*\R # reach the first occurrence of SECTION2 from the start
)
(?>.*\R)*? # match lazily eventual lines that don't start with ON
\K         # remove all on the left from the match result
ON\h       # and keep only ON with a trailing space

(?!\A) 禁止第一个分支在字符串的开头成功,这样第一个匹配总是使用第二个分支(只有一次,因为它以 \A 开头)。接下来的比赛总是使用第一个分支。这会强制所有出现的 ON 出现在 SECTION2 之后。