查找一个不在前面的regexp模式



我有以下HTML文件结构:

<table>
   <tr class="heading">
      <td colspan="2">
         <h2 class="groupheader">Public Types</h2> 
         <!-- I don't want that! We're in a table.-->
      </td>
   </tr>
   <tr>...</tr> 
</table>
<h2 class="groupheader">Detailed Description</h2>
  <!-- I want all that until the next h2-->
  <div class="textblock"><p>Provides the functions to control the generation of a single data log file. </p>
    <h4>Example</h4>
    <div class="fragment"><div class="line">Test <a href="aaa">stuff</a>();</div>
        <div class="line">...</div>     
        <div class="line">...</div>
    </div>
</div> <!-- end of first result -->
<h2 class="groupheader">Member</h2>
<!-- I want all that until the next h2 or hr-->
<a class="anchor"></a>
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">enum <a class="el" href="...">test</a></td>
        </tr>
      </table>
</div><div class="memdoc">
<hr><!-- End of 2nd result -->

使用Regexp,我需要获取每个标题之间的所有内容,直到下一个标题或hr标记,如果它是表中的,则除外。

到目前为止,我已经掌握了我所有的h2->h2|hr内容。它就像:

(?s)(<h2 class="groupheader">.*?)(<h2|<hr)

如何跳过表中H2下的内容?我试着用消极的眼神来掩饰自己,但我没有取得任何进展。

谢谢你的帮助。

注意,HTML应该用合适的解析器进行解析器

现在,由于我们只剩下看起来像HTML的输入和任务

要获取每个标题之间的所有内容,直到下一个标题或hr标签,请期待它是否是表中的

让我展示一下如何做到这一点。

您可以在回火贪婪令牌((?:(?!</table|<h2|<hr)(?:<tableb[^<]*>.*?</table>|.))*)的帮助下获得所需的子字符串(该令牌与之前没有在负前瞻中启动任何备选方案的任何符号相匹配,从而将匹配保持在<table>边界内,并与内部表相匹配(,并在末尾进行正前瞻:

(?s)<h2 class="groupheader">[^<]*</h2>s*((?:(?!</table|<h2|<hr)(?:<tableb[^<]*>.*?</table>|.))*)(?=<h2|<hr)

请参阅演示。

请注意,您可以使用hd+来支持任何级别的h,而不是h2

最新更新