The main function is: Replace the value of the img tag src in the html string.
The code is as follows:
package com.junlenet.common.util;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * html processing tool class* @author huweijun * @date July 13, 2016 at 7:25:09 pm */public class HtmlUtils {/** * Replace the attributes and values of the specified tag* @param str String to be processed* @param tagTag Name* @param tagAttrib The tag attribute value to be replaced* @param startTag New tag start tag* @param endTag New tag end tag* @return * @author huweijun * @date July 13, 2016 at 7:15:32 pm */public static String replaceHtmlTag(String str, String tag, String tagAttrib, String startTag, String endTag) {String regxpForTag = "<//s*" + tag + "//s+([^>]*)//s*" ;String regxpForTagAttrib = tagAttrib + "=//s*/"([^/"]+)/"" ;Pattern patternForTag = Pattern.compile (regxpForTag,Pattern.CASE_INSENSITIVE );Pattern patternForAttrib = Pattern.compile (regxpForTagAttrib,Pattern.CASE_INSENSITIVE ); Matcher matcherForTag = patternForTag.matcher(str);StringBuffer sb = new StringBuffer();boolean result = matcherForTag.find();while (result) {StringBuffer sbreplace = new StringBuffer( "<"+tag+" ");Matcher matcherForAttrib = patternForAttrib.matcher(matcherForTag.group(1)); if (matcherForAttrib.find()) { String attributeStr = matcherForAttrib.group(1); matcherForAttrib.appendReplacement(sbreplace, startTag + attributeStr + endTag); } matcherForAttrib.appendTail(sbreplace); matcherForTag.appendReplacement(sb, sbreplace.toString()); result = matcherForTag.find();}matcherForTag.appendTail(sb); return sb.toString();} public static void main(String[] args) { StringBuffer content = new StringBuffer(); content.append("<ul class=/"imgBox/"><li><img id=/"160424/" src=/"uploads/allimg/160424/1-160424120T1-50.jpg/" class=/"src_class/"></li>"); content.append("<li><img id=/"150628/" src=/"uploads/allimg/150628/1-15062Q12247.jpg/" class=/"src_class/"></li></ul>"); System.out.println("The original string is:"+content.toString()); String newStr = replaceHtmlTag(content.toString(), "img", "src", "src=/"http://junlenet.com/", "/""); System.out.println(" Replace it as:"+newStr); } }The above method of replacing the src value in the IG tag in Java regular script is all the content I have shared with you. I hope you can give you a reference and I hope you can support Wulin.com more.