Java去掉字符串中所有的標簽,獲取純文本內容

gaok1 8年前發布 | 3K 次閱讀 Java
 public class Test {
 /**
  * @Title: main 
  * @Description:
  * @param args 
  * @author 
  * @date 2016年2月17日
  * 1、去掉字符串中所有的標簽,獲取純文本內容
  * 2、獲取html節點中img的src路徑
  */
 public static void main(String[] args) {
  String html = "<div><p style='color:red;'>12132第一串字符</p></div><br /><div><p>這是第二竄字符</p></div><img width='199' src='_image/12/label'/><img width='199' src='_image/13/label'/><img width='199' src='_image/14/label'/>";
  Pattern p = Pattern.compile("<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>");
  Matcher m = p.matcher(html);
  List<String> srcs = new ArrayList<String>();
        while(m.find()){
            srcs.add(m.group(1));
        }
  String regex = "<[^>]*>";
  String str = html.replaceAll(regex, "");
  System.out.println(str+"\n"+srcs.get(0));
 }
}
 本文由用戶 gaok1 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!