java替换超级链接中的url

public static String A(String str) {
		String reg = "(?<=href=\").*?(?=\")";
		Pattern pattern = Pattern.compile(reg);
		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 reg = "]*>(.*?)";
		Pattern pattern = Pattern.compile(reg);
		Matcher matcher = pattern.matcher(str);
		while(matcher.find()){
                        // group(1)是url链接
                        // group(2)是文本
			String hrefStr = matcher.group(1);
			String shortDomainGetUrl = "http://url.hexun.com/tiny?url=" + hrefStr;
			String data = HTTPUtils.sendGet(shortDomainGetUrl);
			if(StringUtil.isBlank(data)){
				str = str.replace(hrefStr, data);
			}
		}
		return str;

 

你可能感兴趣的:(工具集合)