Java 移除html,图片 链接转文字

public static String removeHTML(String content) {
		content = content.replaceAll("

", "\r\n"); //

替换为换行 content = content.replaceAll("", "\r\n"); String regex = "<[^><]+([><])"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(content); ArrayList list = new ArrayList(); int pos = 0; while(matcher.find(pos)){ String[] strs = new String[matcher.groupCount()+1]; for(int i=0; i<=matcher.groupCount(); i++) { strs[i] = matcher.group(i); } pos = content.indexOf(matcher.group(0), pos)+strs[0].length(); if(strs[1].equals("<")) pos -= 1; list.add(strs); } String txt; for(int i=0;i]+)[\\s\"']?"; txt = " (图片:"; } else if(s.startsWith("]+)[\\s\"']?"; txt = " (链接:"; if(list.size()>i+1 && list.get(i+1)[0].toLowerCase().startsWith("pos0) { txt = " (链接“"+content.substring(pos0, pos1)+"”:"; pos1 += list.get(i+1)[0].length(); content = content.replace(content.substring(pos0, pos1), ""); } } } } if(txt.length()>1) { pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); matcher = pattern.matcher(strs[0]); if(matcher.find()) { String[] strs1 = new String[matcher.groupCount()+1]; for(int ii=0; ii<=matcher.groupCount(); ii++) { strs1[ii] = matcher.group(ii); } if(strs1[1].length()>1) { txt += strs1[1]+" ) "; }else txt = ""; } else txt = ""; } content = content.replace(strs[0], txt); } return content; }

 

你可能感兴趣的:(Java)