正则表达式在表中排除表达式

时间:2014-04-05 19:00:29

标签: html ruby-on-rails ruby regex

我需要捕获一个表达式,但前提是它不在表中。我正在使用Ruby。

hello.
<p>
  <b>  1 capture  </b>
</p>
<table class="tb1">
  <tr>
    <td>
      <p>
        <b> 1 don't capture </b>
      </p>
    </td>
  </tr>
</table>
<p><strong> 2 capture </strong></p>
<table><tr>  <td>  <p>  <b>  2 don't capture  </b>  </p>  </td>  </tr>  </table>
<p>  <b>  3 don't capture  </b>  </p> Some text here...
<p>  <strong>  4 don't capture  </strong> Some text here... </p>
<table fdfdfdfd>
<tr>
  <td>
    <p>  <b>  5 don't capture  </b>  </p>
  </td>
  <table>
    table...
  </table>
 </tr>
</table>

结果应该是:

<p>
  <b>  1 capture  </b>
</p>
<p><strong> 2 capture </strong></p>

目前我有这个正则表达式:\n\s*<p>\s*<(strong|b)>(?:(?!</\1>).)+</\1>\s*</p>\s*\n

如果前面有</table>并且没有<table.*>,则尝试排除表达式,但是贪婪表达式和新行问题存在一些复杂问题。

编辑:我知道还有其他方法可以做到这一点,但我必须知道这是否可能与正则表达式。

1 个答案:

答案 0 :(得分:0)

是的,您可以进行递归正则表达式匹配。这是一个指向如何:Rexegg regex-recursion的指针。 Ruby recursive regex matching

有一个SO答案
相关问题