正则表达式匹配两个单词并获取所有出现的地方

RegEx match between two words and get all occurrences

我正在使用 RegEx 清理 XML 个包含 HTML 的文件。

有些文件包含多个 style 元素,我想将它们和中间的内容全部删除。例如:

(Test here on regex101...)

<STYLE>
   group 1
</STYLE>
   Random text here which shall not be removed.
<STYLE>
   group 2
</STYLE>
   Some more random text here which shall not be removed.
<STYLE>
   group 3
</STYLE>

我正在使用以下带有 /s 参数的 RegEx

(<STYLE>).*(<\/STYLE>)

问题在于此 RegEx 将匹配 <style> (#1) 和最后一个 </style> (#3) 之间的所有内容。

我只想匹配 group<style></style> 元素。如何实现?

您可以尝试使用 ? 运算符使正则表达式不贪婪:

(&lt;STYLE&gt;).*?(&lt;\/STYLE&gt;)
                ^^^ use ? to tell the regex engine to stop at the first closing tag

此处演示:

Regex101