在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

Java去掉字符串中所有的标签,获取纯文本内容

2016-12-20 13:16| 发布者: zhangjf| 查看: 579| 评论: 0

摘要: public class Test { /** * @Title: main * @Description: * @param args * @author * @date 2016年2月17日 * 1、去掉字符串中所有的标签,获取纯文本内容 * 2、获取html节点中img的src路径 */ public st ...
  1. public class Test {
  2. /**
  3. * @Title: main
  4. * @Description:
  5. * @param args
  6. * @author
  7. * @date 2016年2月17日
  8. * 1、去掉字符串中所有的标签,获取纯文本内容
  9. * 2、获取html节点中img的src路径
  10. */
  11. public static void main(String[] args) {
  12. 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'/>";
  13. Pattern p = Pattern.compile("<img[^>]+src\s*=\s*['"]([^'"]+)['"][^>]*>");
  14. Matcher m = p.matcher(html);
  15. List<String> srcs = new ArrayList<String>();
  16. while(m.find()){
  17. srcs.add(m.group(1));
  18. }
  19. String regex = "<[^>]*>";
  20. String str = html.replaceAll(regex, "");
  21. System.out.println(str+"n"+srcs.get(0));
  22. }
  23. }
复制代码

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-8 18:21

Copyright 2015-2025 djqfx

返回顶部