java正则过滤整个div

本文介绍如何使用Java的正则表达式来高效地过滤和提取HTML文档中的div元素内容。通过实例代码和解析过程,理解正则在处理HTML文本时的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. (?i) 表示所在位置右侧的表达式开启忽略大小写模式
2. Pattern.CASE_INSENSITIVE | Pattern.DOTALL 进行大小不明感的匹配单行或多行文本
例如:
<div id="content">
    <div class="divBlock" id="0.24911554699056548">
      <div class="edit-boxfull" data-tooltype="top">
        <div class="edit-boxRightIcon">
           <i><span class="glyphicon glyphicon-move f16" title="拖动"></span></i>
           <i><span class="glyphicon glyphicon-edit f16" title="编辑"></span></i>
           <i><span class="glyphicon glyphicon-file f16" title="复制"></span></i>
           <i><span class="glyphicon glyphicon-trash f16" title="删除"></span></i>
        </div>
        <div class="edit-contentBox">
            <table border="0" cellpadding="0" cellspacing="0" width="100%" class="Tem_FullBlock">
             <tbody>
              <tr>
                 <td align="left" valign="top" style="padding:10px 0;">
                    <table border="0" cellpadding="0" cellspacing="0" width="100%">
                       <tbody>
                        <tr>
                          <td valign="top" class="Tem_TextContent" style="color:#232425; font-family:Microsoft YaHei; font-size:12px; line-height:1.4; text-align:center; mso-line-height-rule:exactly;">图片无法显示? 请点击 <a href="http://image.izacholsm.com/t/zz?t=D49DE87F-74DB-4B3D-A46C-C71BD5B37923&amp;STARID={{{STARID}}}" style="color:#565656; text-decoration:underline;" target="_blank">在线浏览</a></td>
                       </tr>
                     </tbody>
                   </table>
                  </td>
              </tr>
             </tbody>
            </table>
        </div>
      </div>
    </div>
</div>
去掉内容后显示为:
<div id="content">
  <table border="0" cellpadding="0" cellspacing="0" width="100%" class="Tem_FullBlock">
     <tbody>
      <tr>
         <td align="left" valign="top" style="padding:10px 0;">
            <table border="0" cellpadding="0" cellspacing="0" width="100%">
               <tbody>
                <tr>
                  <td valign="top" class="Tem_TextContent" style="color:#232425; font-family:Microsoft YaHei; font-size:12px; line-height:1.4; text-align:center; mso-line-height-rule:exactly;">图片无法显示? 请点击 <a href="http://image.izacholsm.com/t/zz?t=D49DE87F-74DB-4B3D-A46C-C71BD5B37923&amp;STARID={{{STARID}}}" style="color:#565656; text-decoration:underline;" target="_blank">在线浏览</a></td>
               </tr>
             </tbody>
           </table>
          </td>
      </tr>
     </tbody>
  </table>
</div>


//过滤内部菜单div: "edit-boxfull"
Pattern patternDiv = Pattern.compile("<div class=\"(?i)edit-box(top|left|right|bottom|topblock).*?</div>");
Matcher matcherDiv = patternDiv.matcher(body);
while (matcherDiv.find()) {
    body=body.replace(matcherDiv.group(),"");
}

//针对 "divBlock",仅返回"Tem_FullBlock"组合在一起的值
patternDiv = Pattern.compile("(<div\\sclass=\"divBlock((?!divBlock).)*\\/div>)", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
matcherDiv = patternDiv.matcher(body);
while (matcherDiv.find()) {
    String div=matcherDiv.group();
    Pattern patternTable = Pattern.compile("(<table.*class=\"Tem_FullBlock.*</table>)", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
    Matcher matcherTable = patternTable.matcher(div);
    if (matcherTable.find()) {
        body=body.replace(div,matcherTable.group(1));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值