Regex 提取记事本中两个特定单词之间的字符串/数据++;在多行数据中

Regex 提取记事本中两个特定单词之间的字符串/数据++;在多行数据中,regex,notepad++,Regex,Notepad++,全部, 我一直在尝试使用RegEx Search and Replace在Notepad++中提取两个特定单词之间的文本,但没有成功 它给了我最后一次找到的匹配,我尝试了搜索堆栈溢出,并完成了几个问题,但没有运气,我的数据是错误的 Open options for my word1 My Text1 My Text1 Second Line My Text1 Third Line Word2 My Fixed Text Word3 Open options for my word1

全部,

我一直在尝试使用RegEx Search and Replace在Notepad++中提取两个特定单词之间的文本,但没有成功

它给了我最后一次找到的匹配,我尝试了搜索堆栈溢出,并完成了几个问题,但没有运气,我的数据是错误的

Open options for my word1
 My Text1
My Text1 Second Line
My Text1 Third Line
 Word2 My Fixed Text   Word3

Open options for my word1
  My Text2
My Text2 Second Line
My Text2 Third Line
 Word2 My Fixed Text   Word3

Open options for my word1
 My Text3
My Text3 Second Line
My Text3 Third Line
 Word2 My Fixed Text   Word3

Open options for my word1
 My Text4
My Text4 Second Line
My Text4 Third Line
 Word2 My Fixed Text   Word3

Open options for my word1
 My Text5
My Text5 Second Line
My Text5 Third Line
 Word2 My Fixed Text   Word3

Open options for my word1
 My Text6
My Text6 Second Line
My Text6 Third Line
 Word2 My Fixed Text   Word3

Open options for my word1
 My Text7
My Text7 Second Line
My Text7 Third Line
 Word2 My Fixed Text   Word3
我的正则表达式是
*word1(.*)Word2.*
,我将它替换为$1


它给出了正则表达式匹配的最后一次出现的文本,有人可以查看它并告诉我这里缺少了什么。

您需要使捕获组中的
匹配任何字符,包括换行符:

.*word1((?s:.*?))Word2.*
        ^^^^^^^^
启用点调用标志的
(?s:…)
修改器组将使
匹配任何字符,包括换行符。
匹配换行符必须关闭(参见下面的屏幕截图)。若要使模式在不考虑
匹配换行选项的情况下工作,请使用修改器组,使模式中的每个
都位于以下位置:
(?-s:.*)word1((?-s:.*))Word2(?-s:.*)
(其中
(?-s:…)
在修改器组中打开DOTALL行为)

(?s:.*?
模式的等价物是
[\s\s]*?
[\w\w]*?
[\d\d]*?
),但使用修饰符似乎是解决此问题的一种更为自然的方法

图案细节

  • *
    -除换行符以外的任何字符,尽可能多,直到最后一个字符
  • word1
    -
    word1
    在线
  • ((?s:.*)
    -组1匹配任何0+字符,尽可能少到第一个字符
  • Word2
    -
    Word2
    子字符串和
  • *
    -行的其余部分

我无法在我的系统上复制此问题。您是否勾选了
。匹配换行符
选项?如果不启用匹配换行符,它不会替换任何内容。@RaviMiddha我添加了完整的模式解释。