【问题标题】:Regular Expression Find Tag within another tag正则表达式在另一个标签中查找标签
【发布时间】:2026-01-07 14:15:01
【问题描述】:

我想使用正则表达式查找表标签内的所有 br 标签。这是我目前所拥有的:

<table[^>]*>(((<br/>)(?!</table>)).)*</table>

但是这段代码在notepad++中不起作用。

这是测试正则表达式的内容:

<table>
</table>
<table>
<br/>
</table>

基本上最后 3 行应该用正则表达式找到,但我上面列出的一个正则表达式什么也没找到。

【问题讨论】:

  • 如果有人不明白 j08691 的参考:*.com/questions/1732348/…
  • 除了 p.s.w.g 的参考资料之外,还有人回答这个问题吗

标签: html regex tags


【解决方案1】:

试试

<table(?: [^<>]+)?>(?:(?!</table>).)*<br/>.*?</table>

带有点匹配所有修饰符s

Demo.

解释:

<table # start with an opening <table> tag
(?: [^<>]+)?
>
(?: # then, while...
    (?! #...there's no </table> closing tag here...
        </table>
    )
    . #...consume the next character
)*
<br/> # up to the first <br/>
.*? # once we've found a <br/> tag, simply match anything...
</table> #...up to the next closing </table> tag.

【讨论】: