【问题标题】:Android parse html save href linksAndroid解析html保存href链接
【发布时间】:2015-04-10 03:48:06
【问题描述】:

我有一个 html 字符串,例如:

一个链接是sajfhds iufl

如何将此html字符串转换为包含链接但不包含任何html标签的字符串: 结果应该是:

一个链接是http://image.html

【问题讨论】:

    标签: android html parsing hyperlink href


    【解决方案1】:
    String regex = "^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";
    
    String regex = "\\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";
    
    String regex = "<\\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]>"; // matches <http://google.com>
    
    String regex = "<^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]>"; // does not match <http://google.com>
    

    【讨论】:

      【解决方案2】:

      你会得到类似这样的字符串

      One link is <a href="http://image.html">sajfhds iufl</a>
      

      你需要的是

      One link is <a href="http://image.html">http://image.html</a>
      

      所以,你应该做的是使用下面的代码找到模式

      //imports required
      import java.util.regex.Matcher;
      import java.util.regex.Pattern;
      
              String stringToSearch = "<a href = \"http://image.html\" > sajfhds iufl</a>";
      
              // the pattern we want to search for
              Pattern p = Pattern.compile("<a href\\s*=\\s*\"(.+?)\"\\s*>(.+?)</a>");
              Matcher m = p.matcher(stringToSearch);
      
              if (m.find())
              {
                String temp = stringToSearch.replace(m.group(2), m.group(1)); 
                //use the temp string for display
              }
      

      【讨论】:

        猜你喜欢
        • 2014-10-21
        • 1970-01-01
        • 2017-02-15
        • 2016-05-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-28
        • 2013-07-22
        相关资源
        最近更新 更多