<DIV class="postbody">任意html</div>
这样一段html,我匹配中间的任意html,正则是这样的<DIV class="postbody">(?<key1>.*?)</div>
结果是如果中间部分有div的话,则提前结束了,匹配到红色部分,不能匹配到最后的div
如:
<DIV class="postbody">
<div>sdsd</div>
<div>ddddddddddd</div>sdsdddddddddd
</div>
如何解决?

解决方案 »

  1.   

    一种方法是利用外沿匹配 即写正则时包括外面一层或几层标签作为标记 例如
    string str=System.Text.RegularExpressions.Regex.Replace("string input ",".*<table id='hehe' name='xixi'><DIV class=\"postbody\">(*)?</DIV></table>.*","$1",System.Text.RegularExpressions.RegexOptions.Singleline|System.Text.RegularExpressions.RegexOptions.IgnoreCase);