欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

使用正则表达式处理html标签方案分享_html/css_WEB-ITnose

程序员文章站 2022-04-20 11:41:24
...
首先呢,阅读本文前建议大家去读下这篇文章点击打开链接,是关于java正则工具类Matcher相关的一些探讨和建议

下面这是其中一项关于处理匹配内容替换的范例,

appendReplacement() + appendTail()组合


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标签	private final static String regExpTag = ""; // 找出表情标签	private final static String regATag = "((.*?))"; // 找出标签		public final static String regSpanTag = "(.*?)