2013-07-17 43 views
0

我想从下面的html代码得到softwareVersionMatcher.find()返回false的android

<div class="title">Current Version</div> <div class="content" itemprop="softwareVersion"> 1.1.3 </div> </div> <div class="meta-info"> <div class="title">Requires Android</div> <div class="content" itemprop="operatingSystems">  2.2 and up </div> </div> 

我用下面的代码为

String Html = GetHtml("https://play.google.com/store/apps/details?id="+ AppID) 
Pattern pattern = Pattern.compile("softwareVersion\">[^<]*</dd"); 
Matcher matcher = pattern.matcher(Html); 
matcher.find(); 

String GetHtml(String url1) 
    { 
     String str = ""; 
     try 
     { 
      URL url = new URL(url1); 
      URLConnection spoof = url.openConnection(); 
      spoof.setRequestProperty("User-Agent", 
        "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; H010818)"); 
      BufferedReader in = new BufferedReader(new InputStreamReader(
        spoof.getInputStream())); 
      String strLine = ""; 
      // Loop through every line in the source 
      while ((strLine = in.readLine()) != null) 
      { 
       str = str + strLine; 
      } 
     } 
     catch (Exception e) 
     { 
     } 
     return str; 
    } 

但匹配总是返回false。我认为我有问题模式任何人都可以请求帮助我 谢谢

+1

使用HTML解析器如[JSoup](http://jsoup.org/) – Reimeus

+0

怎么样?我不知道。下面的代码有模式问题,我不太了解模式。 – user2582308

+0

使用上面的链接阅读。 – Reimeus

回答

0

正如别人所评论的,我通常会使用一个html解析器从html中提取东西。然而,在你只是从一个字符串中抽出一点信息的情况下,我可以看到你为什么要使用正则表达式。

你需要做的就是这样的事情 - 与您的正则表达式的问题是一个额外的d。另外,如果您将括号中的内容放在括号内,则可以使用.group来抓取它。

import java.util.regex.*; 

public class R { 

    public static void main(String[] args){ 
    String Html = "<div class=\"title\">Current Version</div> <div class=\"content\" itemprop=\"softwareVersion\"> 1.1.3 </div> </div> <div class=\"meta-info\"> <div class=\"title\">Requires Android</div> <div class=\"content\" itemprop=\"operatingSystems\">  2.2 and up </div> </div>"; 

    Pattern pattern = Pattern.compile("softwareVersion\">([^<]*)</d"); 
    Matcher matcher = pattern.matcher(Html); 
    System.out.println(matcher.find()); 
    System.out.println(matcher.group(1)); 
    } 
} 
+0

仍不能工作Matcher.find()返回false。 – user2582308

+0

当我运行这段代码时,我得到'true',然后'1.1.3'。你如何运行它? – selig