时间:2021-07-01 10:21:17 帮助过:12人阅读
下面这是其中一项关于处理匹配内容替换的范例,
import java.util.regex.Pattern;import java.util.regex.Matcher;public class MatcherReplaceExample { public static void main(String[] args) { String text = "John writes about this, and John Doe writes about that," + " and John Wayne writes about everything." ; String patternString1 = "((John) (.+?)) "; Pattern pattern = Pattern.compile(patternString1); Matcher matcher = pattern.matcher(text); StringBuffer stringBuffer = new StringBuffer(); while(matcher.find()){ matcher.appendReplacement(stringBuffer, "Joe Blocks "); System.out.println(stringBuffer.toString()); } matcher.appendTail(stringBuffer); System.out.println(stringBuffer.toString()); }}
在这里和大家分享本人写的一个工具类,使用正则表达式处理一些常用html标签,这种标签其实可以定制,只需找出与其匹配的正则表达式,然后按照推荐处理方案去处理即可
package com.jieve.util;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * @ClassName: HtmlRegexpUtil * @Description: 处理html标签 * @author YYYong * @date 2016年5月18日 上午9:26:28 * */public class HtmlRegexpUtil{ private final static String regxpForHtml = "<([^>]*)>"; // 过滤所有以<开头以>结尾的标签 private final static String rexImgTag = "<(img.*?)(class.*?)(src.*?)>"; // 找出IMG标签 private final static String regExpTag = "<(img.*?)(src.*?)(alt.*?)(title.*?)(height.*?)>"; // 找出表情标签 private final static String regATag = "(<(a.*?href=.*?)>(.*?))"; // 找出标签 public final static String regSpanTag = "<(span.*?)(style.*?)>(.*?)"; // 找出标签 public final static String regStrongTag = "(<(a.*?href=.*?)>(.*?))"; // 找出标签 private final static String spanTagPre = ""; // 找出标签 private final static String spanTagSuf = ""; // 找出标签 private final static String strongTagPre = ""; // 找出标签 private final static String strongTagSuf = ""; // 找出标签 /** * @Title: handleImgTag * @Description: 处理IMG标签 * @param @param str * @param @param prefix * @param @param suffix * @param @return 设定文件 * @return String 返回类型 * @throws */ public static String handleImgTag(String str, String prefix, String suffix) { Pattern pattern = Pattern.compile(rexImgTag); Matcher matcher = pattern.matcher(str); StringBuffer sb = new StringBuffer(); boolean result = matcher.find(); while (result) { String temp = matcher.group(3); temp = temp.substring(6); matcher.appendReplacement(sb, prefix + temp.substring(0, temp.length() - 1) + suffix); result = matcher.find(); } matcher.appendTail(sb); return sb.toString(); } /** * @Title: handleExpTag * @Description: 处理表情标签 * @param @param str * @param @param prefix * @param @param suffix * @param @return 设定文件 * @return String 返回类型 * @throws */ public static String handleExpTag(String str, String prefix, String suffix) { Pattern pattern = Pattern.compile(regExpTag); Matcher matcher = pattern.matcher(str); StringBuffer sb = new StringBuffer(); boolean result = matcher.find(); while (result) { String temp = matcher.group(4); temp = temp.substring(7, temp.length() - 2); matcher.appendReplacement(sb, prefix + temp + suffix); result = matcher.find(); } matcher.appendTail(sb); return sb.toString(); } /** * @Title: handleATag * @Description: 处理标签 * @param @param str * @param @param prefix * @param @param suffix * @param @return 设定文件 * @return String 返回类型 * @throws */ public static String handleATag(String str, String prefix, String suffix) { Pattern pattern = Pattern.compile(regATag); Matcher matcher = pattern.matcher(str); StringBuffer sb = new StringBuffer(); boolean result = matcher.find(); while (result) { String temp = matcher.group(3); if(temp.startsWith("$")) { // $符号为正则特殊符号,需替换后再处理 temp = "[@@]" + temp.substring(1, temp.length() - 1) + "[@@]"; } matcher.appendReplacement(sb, prefix + temp + suffix); result = matcher.find(); } matcher.appendTail(sb); return sb.toString().replace("[@@]", "$"); } /** * @Title: removeTag * @Description: 移除标签 * @param @param str * @param @param tag * @param @return 设定文件 * @return String 返回类型 * @throws */ public static String removeTag(String str,String tag) { int count = 2; if(regSpanTag.equals(tag)) { count++; } Pattern pattern = Pattern.compile(tag); Matcher matcher = pattern.matcher(str); StringBuffer sb = new StringBuffer(); boolean result = matcher.find(); while (result) { String temp = matcher.group(count); matcher.appendReplacement(sb,temp); result = matcher.find(); } matcher.appendTail(sb); if(count == 2) { return sb.toString().replace(strongTagPre, "").replace(strongTagSuf, ""); } else { return sb.toString().replace(spanTagPre, "").replace(spanTagSuf, ""); } } /** * @Title: replaceTag * @Description: 替换标记以正常显示 * @param @param input * @param @return 设定文件 * @return String 返回类型 * @throws */ 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()); } /** * @Title: hasSpecialChars * @Description: 判断标记是否存在 * @param @param input * @param @return 设定文件 * @return boolean 返回类型 * @throws */ 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; } /** * @Title: filterHtml * @Description: 基本功能:过滤所有以"<"开头以">"结尾的标签 * @param @param str * @param @return 设定文件 * @return String 返回类型 * @throws */ 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(); } /** * @Title: fiterHtmlTag * @Description: 过滤指定标签 * @param @param str * @param @param tag * @param @return 设定文件 * @return String 返回类型 * @throws */ public static String fiterHtmlTag(String str, String tag) { 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, ""); result1 = matcher.find(); } matcher.appendTail(sb); return sb.toString(); }}
String content = "测试测试测试测试啦啦啦啦啦啦啦啊\n测试啦啦啦啦啦啦啦啦啦阿里啦\n@梦小胖 \n@萌二胖 \n$贵州茅台(SH600519)$ \n$的上次你说的承诺(12580)$ \n\nxiaoming 下划线鲜花县\n\n\n下划线下划线加粗\n"; System.out.println("The original content : " + content); content = HtmlRegexpUtil.handleImgTag(content, "{{", "}}"); System.out.println("The 1st content : " + content); content = HtmlRegexpUtil.handleExpTag(content, "", ""); System.out.println("The 2ed content : " + content); content = HtmlRegexpUtil.handleATag(content, "<", ">"); System.out.println("The 3rd content : " + content); content = HtmlRegexpUtil.removeTag(content, HtmlRegexpUtil.regSpanTag); System.out.println("The 4th content : " + content); content = HtmlRegexpUtil.removeTag(content, HtmlRegexpUtil.regStrongTag); System.out.println("The final content : " + content);
The original content : 测试测试测试测试啦啦啦啦啦啦啦啊测试啦啦啦啦啦啦啦啦啦阿里啦@梦小胖 @萌二胖 $贵州茅台(SH600519)$ $的上次你说的承诺(12580)$ xiaoming 下划线鲜花县下划线下划线加粗The 1st content : 测试测试测试测试啦啦啦啦啦啦啦啊测试啦啦啦啦啦啦啦啦啦阿里啦@梦小胖@萌二胖$贵州茅台(SH600519)$$的上次你说的承诺(12580)${{/192.168.1.20//static/img/picture/835/83592167b2d54d9496be896b94d1998b_originThu.jpg}}xiaoming下划线鲜花县 下划线下划线加粗The 2ed content : 测试测试测试测试啦啦啦啦啦啦啦啊测试啦啦啦啦啦啦啦啦啦阿里啦@梦小胖@萌二胖$贵州茅台(SH600519)$$的上次你说的承诺(12580)${{/192.168.1.20//static/img/picture/835/83592167b2d54d9496be896b94d1998b_originThu.jpg}} xiaoming下划线鲜花县 下划线下划线加粗The 3rd content : 测试测试测试测试啦啦啦啦啦啦啦啊测试啦啦啦啦啦啦啦啦啦阿里啦<@梦小胖>@萌二胖<$贵州茅台(SH600519)$>$的上次你说的承诺(12580)${{/192.168.1.20//static/img/picture/835/83592167b2d54d9496be896b94d1998b_originThu.jpg}} xiaoming下划线鲜花县 下划线下划线加粗The 4th content : 测试测试测试测试啦啦啦啦啦啦啦啊测试啦啦啦啦啦啦啦啦啦阿里啦<@梦小胖>@萌二胖<$贵州茅台(SH600519)$>$的上次你说的承诺(12580)${{/192.168.1.20//static/img/picture/835/83592167b2d54d9496be896b94d1998b_originThu.jpg}}xiaoming下划线鲜花县下划线下划线加粗The final content : 测试测试测试测试啦啦啦啦啦啦啦啊测试啦啦啦啦啦啦啦啦啦阿里啦<@梦小胖>@萌二胖<$贵州茅台(SH600519)$>$的上次你说的承诺(12580)${{/192.168.1.20//static/img/picture/835/83592167b2d54d9496be896b94d1998b_originThu.jpg}}xiaoming下划线鲜花县下划线下划线加粗
作者声明:有问题请联系794465942@qq.com