I have a pretty huge string (a big chunk of html), in which I'd like to find a chunk according to this scenario:
我有一个非常庞大的字符串(一大块html),根据这个场景,我想找到一个块:
<h2>Some text here</h2>
<p>Lorem ipsum... Lorem ipsum... String1... Lorem ipsum...</p>
<p>More Lorem ipsum... More Lorem ipsum...</p>
<h2>Some more text here</h2>
<p>Lorem ipsum... Lorem ipsum... String2... Lorem ipsum...</p>
<p>More Lorem ipsum... More Lorem ipsum...</p>
<h2>Another chunk here, same string</h2>
<p>Lorem ipsum... Lorem ipsum... String2... Lorem ipsum...</p>
<p>More Lorem ipsum... More Lorem ipsum...</p>
<h2>And even more text here</h2>
<p>Lorem ipsum... Lorem ipsum... String3... Lorem ipsum...</p>
<p>More Lorem ipsum... More Lorem ipsum...</p>
I'd like to find the last chunk, starting with the h2 and ending before the next h2, and which includes "String2", which in the example above would be
我想找到最后一个块,从h2开始并在下一个h2之前结束,其中包括“String2”,在上面的示例中将是
<h2>Another chunk here, same string</h2>
<p>Lorem ipsum... Lorem ipsum... String2... Lorem ipsum...</p>
<p>More Lorem ipsum... More Lorem ipsum...</p>
Can anybody help me with this? I use PHP's preg-flavour of RegEx. I get stuck after the
任何人都可以帮我吗?我使用PHP的PreEx风格的RegEx。我被卡住了
<h2(.*+)String2<h2/im
and cannot get my head around how to find the last one only.
并且无法理解如何找到最后一个。
Thanks!
谢谢!
1 个解决方案
#1
2
You can use an approach like this:
你可以使用这样的方法:
<h2>(?:[^\0](?!<h2>))*?String2[^\0]*?(?=<h2>)
正则表达式住在这里。
#1
2
You can use an approach like this:
你可以使用这样的方法:
<h2>(?:[^\0](?!<h2>))*?String2[^\0]*?(?=<h2>)
正则表达式住在这里。