正则表达式过滤html标签
/** * 过滤html标签 * @param s - html字符串 * @param tag - a,p,img,div,(不区分大小写) * @param contain - 是否过滤掉标签内包含的内容 * @return */ public static String filterTag(String s, String tag,boolean contain) { // String regexp = "<\\s*"+tag+"\\s*([^>]*)\\s*>|</\\s*"+tag+"\\s*>"; // String regexp = "<\\s*"+tag+"\\s*([^>]*)>|</\\s*"+tag+"\\s*>"; // String regexp = "<\\s*"+tag+"\\s*([^>]*)>[\\S\\s]*</\\s*"+tag+"\\s*>"; String regexp = null; if(contain){ //懒惰匹配的加问号,如*?,最小匹配 regexp = "<[\\s]*?"+tag+"[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?"+tag+"[\\s]*?>"; //regexp = "<\\s*"+tag+"\\s*([^>]*)>[\\S\\s]*</\\s*"+tag+"\\s*>"; }else{ regexp = "<\\s*"+tag+"\\s*([^>]*)>|</\\s*"+tag+"\\s*>"; } Pattern pattern = Pattern.compile(regexp,Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(s); StringBuffer sb = new StringBuffer(); while (matcher.find()) { matcher.appendReplacement(sb, ""); // System.out.println(matcher.group()); } matcher.appendTail(sb); return sb.toString(); }
相关推荐
wangzhaotongalex 2020-10-20
wyq 2020-11-11
TLROJE 2020-10-26
风雨断肠人 2020-10-13
duanqingfeng 2020-09-29
rechanel 2020-11-16
cshanzhizi 2020-10-16
luofuIT成长记录 2020-09-22
phphub 2020-09-10
taomengxing 2020-09-07
MaggieRose 2020-08-19
flyingssky 2020-08-18
山水沐光 2020-08-18
jyj00 2020-08-15
AHuqihua 2020-08-09
山水沐光 2020-08-03