获取html字符串中第一张图片的路径以及获取html字符串中的文字内容(去掉标签)

/**
	 * 获取html字符串中第一张图片的路径
	 * @param htmlcontent
	 * @return
	 */
	public static String getImgFromHtml(String htmlcontent){
		if(htmlcontent!=null){
			String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>"; 
			  Pattern p_image = Pattern.compile(regEx_img,Pattern.CASE_INSENSITIVE);
	         Matcher m_image = p_image.matcher(htmlcontent); 
	         if(m_image.find()){
	             String img = m_image.group(0);   
	             Matcher m  = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
	              if(m.find()){
	              	if(m.group(0)!=null){
	              		return m.group(0).substring(5, m.group(0).length()-1);
	              	}
	              }
		  }
		}
       return "";
	}
	/**
	 * 获取html字符串中的文字内容(去掉标签)
	 * @param htmlcontent
	 * @return
	 */
	public static String getContentFromHtml(String htmlcontent){
		if(htmlcontent!=null){
			return htmlcontent.replaceAll("<\\/?.+?>", "");
		}
		return "";
	}

 

posted @ 2014-12-11 10:02  青木流水  阅读(792)  评论(0编辑  收藏  举报