开发者

RegEx: h1 followed by h2 without p in between

I need a regular expression to find out whether or not a h1 tag i开发者_JAVA技巧s followed by a h2 tag, without any paragraph elements in between. 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.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜