过滤html标签,保留指定参数

业务需求如下:规则内容需要导出到excel中,需要包含设置的参数。

实现思路:正则过滤html标签。

分析:如下为需要导出的规则内容。通过分析 需要过滤类型为A:input 文本类型如1、B:input radio(数据没有)类型 C:select multiple类型如2

1、非营业且投保人的投保人名字 不包含 <input class="input" type="text"  id="AIC_004^字符参数1"  name="AIC_004^字符参数1" value="运输,货运,储运,物流,出租,租赁,客运,旅游,搬家,汽车服务"  > 中任一字符(逗号分隔) 并且 被保险人的名称 不包含 <input class="input" type="text"  id="AIC_004^字符参数2"  name="AIC_004^字符参数2" value="运输,货运,储运,物流,出租,租赁,客运,旅游,搬家,汽车服务"  > 中任一字符(逗号分隔) 并且 行驶证车主的名称 不包含 <input class="input" type="text"  id="AIC_004^字符参数3"  name="AIC_004^字符参数3" value="运输,货运,储运,物流,出租,租赁,客运,旅游,搬家,汽车服务"  > 中任一字符(逗号分隔)

2、车船税收费方式 包含 <select  name="AIC_157^收费方式多选参数1" multiple="multiple"   ><option value="349002001" selected >正常纳税(349002001)</option><option value="349002002">已经完税(349002002)</option><option value="349002003">免税(349002003)</option><option value="349002004">减免税(349002004)</option><option value="349002006">新车缓税(349002006)</option><option value="349002007">纳税并补税(349002007)</option></select> 中任一字符(逗号分隔)

思路:1、处理radio类型 2、处理option类型 3、队列取值 4、分割挖坑给值跳 5、过滤所以html标签

  /**
     * 讲含有html标签的的规则内容去掉html,保留参数
     * @param source
     * @return
     */
    public static String Finalfiter(String source)
    {
    	String inputTag ="input";
    	String optionTag ="option";
    	//分割标签,取个长的,预防冲突
    	String splitTag="--------";
    	//value属性
    	String attr ="value";
    	String str =null;
    	//1、 如果有 radio 将 
   
   
    
     valueQueue = matchFecthValue(str, inputTag, attr); 
    	//4、过滤 
    
    
   
   
执行结果

1、非营业且投保人的投保人名字 不包含 运输,货运,储运,物流,出租,租赁,客运,旅游,搬家,汽车服务 中任一字符(逗号分隔) 并且 被保险人的名称 不包含 运输,货运,储运,物流,出租,租赁,客运,旅游,搬家,汽车服务 中任一字符(逗号分隔) 并且 行驶证车主的名称 不包含 运输,货运,储运,物流,出租,租赁,客运,旅游,搬家,汽车服务 中任一字符(逗号分隔).

2、车船税收费方式 包含 正常纳税(349002001) 中任一字符(逗号分隔).

完整的 工具类参考


import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
  
/**  
 * 

* Title: HTML相关的正则表达式工具类 *

*

* Description: 包括过滤HTML标记,转换HTML标记,替换特定HTML标记 *

*

*

*/ public class HtmlRegexpUtil { private final static String regxpForHtml = "<([^>]*)>"; // 过滤所有以 <开头以> 结尾的标签 @SuppressWarnings("unused") private final static String regxpForImgTag = "<\\s*img\\s+([^>]*)\\s*>"; // 找出IMG标签 @SuppressWarnings("unused") private final static String regxpForHtmlValue = "value=\"([^\"]+)\""; //找出html value 属性 @SuppressWarnings("unused") private final static String regxpForImaTagSrcAttrib = "src=\"([^\"]+)\""; // 找出IMG标签的SRC属性 /** * */ public HtmlRegexpUtil() { } /** * 获取指定HTML标签的指定属性的值 * @param source 要匹配的源文本 * @param element 标签名称 * @param attr 标签的属性名称 * @return 属性值列表 */ public static List match(String source, String element, String attr) { List result = new ArrayList (); String reg = "<" + element + "[^<>]*?\\s" + attr + "=['\"]?(.*?)['\"]?(\\s.*?)?>"; Matcher m = Pattern.compile(reg).matcher(source); while (m.find()) { String r = m.group(1); result.add(r); } return result; } /** * 获取指定HTML标签的指定属性的值 存放入队列中 * @param source * @param element * @param attr * @return */ public static Queue matchFecthValue(String source ,String element,String attr) { Queue queue=new LinkedList (); String reg = "<" + element + "[^<>]*?\\s" + attr + "=['\"]?(.*?)['\"]?(\\s.*?)?>"; Matcher m = Pattern.compile(reg).matcher(source); while (m.find()) { String r = m.group(1); queue.add(r); } return queue; } /** * 保留指定HTML标签的指定属性的值 * @param source 要匹配的源文本 * @param element 标签名称 * @param attr 标签的属性名称 * @return 属性值列表 */ public static List matchAndkeep(String source, String element, String attr) { List result = new ArrayList (); String reg = "<" + element + "[^<>]*?\\s" + attr + "=['\"]?(.*?)['\"]?(\\s.*?)?>"; Matcher m = Pattern.compile(reg).matcher(source); while (m.find()) { String r = m.group(1); result.add(r); } return result; } /**过滤 * 只保留 value 为 ture * @param source * @param element * @param attr */ public static String fiterHtmlTagContainsRadio(String source, String element, String attr) { StringBuffer sb = new StringBuffer(); String reg = "<" + element + "[^<>]*?\\s" + attr + "=['\"]?(.*?)['\"]?(\\s.*?)?>"; Matcher m = Pattern.compile(reg).matcher(source); while (m.find()) { //选中的 if(m.group().toString().contains("checked")) { String tempValue="*"; m.appendReplacement(sb, " "); } else{ m.appendReplacement(sb, ""); } } m.appendTail(sb); return sb.toString(); } /**过滤 * 只保留 value 为 selected * @param source * @param element option * @param attr */ public static String fiterHtmlTagContainsSelect(String source, String element, String attr) { StringBuffer sb = new StringBuffer(); String reg = "<" + element + "[^<>]*?\\s" + attr + "=['\"]?(.*?)['\"]?(\\s.*?)?>"+"['\"]?(.*?)['\"]?(\\s.*?)?"+" "; Matcher m = Pattern.compile(reg).matcher(source); while (m.find()) { //选中的 if(m.group().toString().contains("selected")) { m.appendReplacement(sb, m.group().toString()); } else{ m.appendReplacement(sb, ""); } } m.appendTail(sb); return sb.toString(); } public static String Finalfiter(String source) { String inputTag ="input"; String optionTag ="option"; String splitTag="--------"; String attr ="value"; String str =null; //1、 如果有 radio 将 valueQueue = matchFecthValue(str, inputTag, attr); //4、过滤 valueQueue = matchFecthValue(str, tag, attr); //3、过滤 str = matchFecthValue(source, "input", "value"); System.out.println(str); System.out.println(Finalfiter(source)); // System.out.println(fiterAndFill(source, "input", "--------", "value")); } //从给定位置读取文件 public static String readFile(String path){ //从给定位置获取文件 File file = new File(path); BufferedReader reader = null; //返回值,使用StringBuffer StringBuffer data = new StringBuffer(); // try { reader = new BufferedReader(new FileReader(file)); //每次读取文件的缓存 String temp = null; while((temp = reader.readLine()) != null){ data.append(temp); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { //关闭文件流 if (reader != null){ try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } return data.toString(); } /** * * 基本功能:替换标记以正常显示 *

* * @param input * @return String */ public String replaceTag(String input) { if (!hasSpecialChars(input)) { return input; } StringBuffer filtered = new StringBuffer(input.length()); char c; for (int i = 0; i <= input.length() - 1; i++) { c = input.charAt(i); switch (c) { case '<': filtered.append("<"); break; case '>': filtered.append(">"); break; case '"': filtered.append("""); break; case '&': filtered.append("&"); break; default: filtered.append(c); } } return (filtered.toString()); } /** * * 基本功能:判断标记是否存在 *

* * @param input * @return boolean */ public boolean hasSpecialChars(String input) { boolean flag = false; if ((input != null) && (input.length() > 0)) { char c; for (int i = 0; i <= input.length() - 1; i++) { c = input.charAt(i); switch (c) { case '>': flag = true; break; case '<': flag = true; break; case '"': flag = true; break; case '&': flag = true; break; } } } return flag; } /** * * 基本功能:过滤所有以"<"开头以">"结尾的标签 * * @param str * @return String */ public static String filterHtml(String str) { Pattern pattern = Pattern.compile(regxpForHtml); Matcher matcher = pattern.matcher(str); StringBuffer sb = new StringBuffer(); boolean result1 = matcher.find(); while (result1) { matcher.appendReplacement(sb, ""); result1 = matcher.find(); } matcher.appendTail(sb); return sb.toString(); } /** * * 基本功能:过滤所有以"<"开头以">"结尾的标签 并替换成"[]" * * @param str * @return String */ public static String filterHtmlAndReplace(String str,String replace) { Pattern pattern = Pattern.compile(regxpForHtml); Matcher matcher = pattern.matcher(str); StringBuffer sb = new StringBuffer(); boolean result1 = matcher.find(); while (result1) { matcher.appendReplacement(sb, replace); result1 = matcher.find(); } matcher.appendTail(sb); return sb.toString(); } /** * * 基本功能:过滤指定标签 并替换为指定 *

* * @param str * @param tag * 指定标签 * @return String */ public static String fiterHtmlTag(String str, String tag,String replaceTag) { String regxp = "<\\s*" + tag + "\\s+([^>]*)\\s*>"; Pattern pattern = Pattern.compile(regxp); Matcher matcher = pattern.matcher(str); StringBuffer sb = new StringBuffer(); boolean result1 = matcher.find(); while (result1) { matcher.appendReplacement(sb, replaceTag); result1 = matcher.find(); } matcher.appendTail(sb); return sb.toString(); } /** * * 基本功能:替换指定的标签 *

* * @param str * @param beforeTag * 要替换的标签 * @param tagAttrib * 要替换的标签属性值 * @param startTag * 新标签开始标记 * @param endTag * 新标签结束标记 * @return String * @如:替换img标签的src属性值为[img]属性值[/img] */ public static String replaceHtmlTag(String str, String beforeTag, String tagAttrib, String startTag, String endTag) { String regxpForTag = "<\\s*" + beforeTag + "\\s+([^>]*)\\s*>"; String regxpForTagAttrib = tagAttrib + "=\"([^\"]+)\""; Pattern patternForTag = Pattern.compile(regxpForTag); Pattern patternForAttrib = Pattern.compile(regxpForTagAttrib); Matcher matcherForTag = patternForTag.matcher(str); StringBuffer sb = new StringBuffer(); boolean result = matcherForTag.find(); while (result) { StringBuffer sbreplace = new StringBuffer(); Matcher matcherForAttrib = patternForAttrib.matcher(matcherForTag .group(1)); if (matcherForAttrib.find()) { matcherForAttrib.appendReplacement(sbreplace, startTag + matcherForAttrib.group(1) + endTag); } matcherForTag.appendReplacement(sb, sbreplace.toString()); result = matcherForTag.find(); } matcherForTag.appendTail(sb); return sb.toString(); } }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值