2013-07-17 42 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