2011-10-31 19 views
1

我想創建一個程序,使用正則表達式從網站抓取數據,然後顯示它。變量無法解析。 Android的正則表達式

我一直在試圖找出發生了什麼,但我不能。

我得到那些三線這個錯誤:M無法解決

while (m.find()) { 
String stateURL = m.group(1); 
String stateName = m.group(2); 


public class VisualizaActivity extends Activity { 
@Override 
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.visualiza); 

    /* Imprime na tela 
    TextView tv = new TextView(this); 
    tv.setText(stateName + "," + stateURL); 
    setContentView(tv); */ 
    } 



String expr = "<td><span\\s+class=\"flagicon\"[^>]*>"; 


public static CharSequence getURLContent(URL url) throws IOException { 
    URLConnection conn = url.openConnection(); 
    String enconding = conn.getContentEncoding(); 
    if (enconding == null){ 
     enconding = "ISO-8859-1"; 
    } 
    BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), enconding)); 
    StringBuilder sb = new StringBuilder(16384); 
    try{ 
     String line; 
     while ((line = br.readLine()) != null){ 
      sb.append(line); 
      sb.append('\n'); 
     } 
    } finally{ 
     br.close(); 
    } 
    return sb; 
} 
public void x() 
{ 
    Pattern patt = Pattern.compile(expr,Pattern.DOTALL | Pattern.UNIX_LINES); 
    try 
    { 
     URL url = new URL("http://en.wikipedia.org/wiki/Mexico"); 
     Matcher m = patt.matcher(getURLContent(url)); 
    } 
    finally 
    {  
    } 
    while (m.find()) { 
     String stateURL = m.group(1); 
     String stateName = m.group(2); 
     System.out.println(stateName + "," + stateURL); 
    } 
} 
} 
+0

除了已經由王子約翰·衛斯理解決你的問題的範圍,你不應該試圖用正則表達式匹配HTML。看看這裏:http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – phlogratos

+0

我認爲你會有更好的時間使用類似XPath的東西,儘管我不確定是否有適用於Java的HTML Agility包(或類似包) –

回答

2

Matcher實例的範圍是在try塊內。

try 
    { 
     URL url = new URL("http://en.wikipedia.org/wiki/Mexico"); 
     Matcher m = patt.matcher(getURLContent(url)); 
     while (m.find()) { 
      String stateURL = m.group(1); 
      String stateName = m.group(2); 
      System.out.println(stateName + "," + stateURL); 
     } 
    } 
    ...