简体   繁体   English

正则表达式:h1后跟h2,中间没有p

[英]RegEx: h1 followed by h2 without p in between

I need a regular expression to find out whether or not a h1 tag is followed by a h2 tag, without any paragraph elements in between. 我需要一个正则表达式来找出h1标签后面是否是h2标签,中间没有任何段落元素。 I tried to use a negative lookahead but it doesn't work: 我尝试使用否定的前瞻,但它不起作用:

<h1(.+?)</h1>(\s|(?!<p))*<h2(.+?)</h2>
<h1((?!</h1).)*</h1>((?!<p).)*<h2

should work. 应该管用。

It matches exactly one h1 tag, then any amount of characters up to the next h2 tag, but only if no p tag is found along the way. 它与第一个h1标签完全匹配,然后与直到下一个h2标签的任意数量的字符匹配,但前提是在此过程中未找到p标签。

Since in this scenario it is rather unlikely that nested tags would occur, this should be quite reliable, even with regex. 因为在这种情况下,嵌套标签不太可能出现,所以即使使用正则表达式也应该非常可靠。

You'll need to activate your tool's/language's option for the dot to match newline characters. 您需要激活工具/语言的选项,点才能匹配换行符。 It might be enough to prefix your regex with (?s) to achieve this. 为您的正则表达式加上(?s)前缀就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM