2012-08-11 32 views
0

我想要一個網頁,加載到一個字符串生成器,使用BufferedReader,然後使用正則表達式來查找和檢索單詞或在這種情況下,一組單詞(部門名稱,如計算機 - 科學,電氣工程等),匹配正則表達式模式。我使用的是java提供的Pattern和Matcher類,但是運行到非法狀態異常。我一直在盯着這段代碼很長一段時間,希望能對問題有一個全新的認識。我知道它與m.find()m.group()方法有關。任何幫助將不勝感激。illegalStateException當使用Java匹配類

我會從輸出我說,它認識到匹配正則表達式的第一個單詞,並開始拋出illegalStateException之後。

我也貼出下面我的代碼:

public class Parser{ 

    static StringBuilder theWebPage; 
    ArrayList<String> courseNames; 
    //ArrayList<parserObject> courseObjects; 

    public static void main(String[] args) 
    { 
     Parser p = new Parser(); 

     theWebPage = new StringBuilder(); 
     try { 
       URL theUrl = new URL("http://ocw.mit.edu/courses/"); 
       BufferedReader reader = new BufferedReader(new InputStreamReader(theUrl.openStream())); 
       String str = null; 

       while((str = reader.readLine())!=null) 
       { 
        theWebPage.append(" ").append(str); 
        //System.out.println(theWebPage); 
       } 
       //System.out.println(theWebPage); 
       reader.close(); 

      } catch (MalformedURLException e) { 
       System.out.println("MalformedURLException"); 

      } catch (IOException e) { 
       System.out.println("IOException"); 
      } 

     p.matchString(); 
    } 

    public Parser() 
    { 
     //parserObject courseObject = new parserObject(); 
     //courseObjects = new ArrayList<parserObject>(); 
     courseNames = new ArrayList<String>(); 
     //theWebPage=" "; 
    } 

    public void matchString() 
    { 
     String matchRegex = "#\\w+(-\\w+)+"; 
     Pattern p = Pattern.compile(matchRegex); 
     Matcher m = p.matcher(theWebPage); 
     int i=0; 
     int x=0; 
     //m.reset(); 

      while(!(m.matches())) 
      { 
       System.out.println("inside matches method " + i); 
       try{ 

         m.find(); 
        x = m.end(); 
        System.out.println(m.group()); 
        PrintStream out = new PrintStream(new FileOutputStream("/Users/xxxx/Desktop/output.txt")); 
        System.setOut(out); 

        //courseNames.add(i,m.group()); 

        i++; 
       }catch(IllegalStateException e) 
       { 
        System.out.println("IllegalStateException"); 
       } catch (FileNotFoundException e) { 
        System.out.println("FileNotFound Exception"); 
       } 
      } 
    } 
} 
+0

更好地解析網頁內容與http://jsoup.org/ – Reimeus 2012-08-11 14:41:30

回答

1

的問題是,你撥打:

x = m.end(); 

即使你可能沒有匹配。爲什麼不將您打電話找()到您的while語句,從而保護聲明也:

while (m.find()) { 
+0

你消除了異常,當我使用你提供的While(m.find())解決方案,但現在它只是識別第一個模式和程序終止沒有錯誤,但輸出是不完整的,因爲文件中還有一些模式沒有被識別。我很抱歉對這個錯誤非常模糊,但我會非常感謝你的幫助。 – anonuser0428 2012-08-11 15:17:44

0

您的解決方案overcomplicates事情有點。這個怎麼樣?

package MitOpenCourseWareCrawler; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

public class Parser { 
    private List<String> courseNames = new ArrayList<String>(); 
    private URL url; 

    public Parser(String url) throws MalformedURLException { 
     this.url = new URL(url); 
    } 

    public static void main(String[] args) throws IOException { 
     Parser parser = new Parser("http://ocw.mit.edu/courses/"); 
     parser.parse(); 
     for (String courseName : parser.courseNames) 
      System.out.println(courseName); 
    } 

    public void parse() throws IOException { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream())); 
     Pattern pattern = Pattern.compile(".*<u>(.+)</u>.*"); 
     Matcher matcher; 
     String line; 
     while ((line = reader.readLine()) != null) { 
      matcher = pattern.matcher(line); 
      if (matcher.matches()) 
       courseNames.add(matcher.group(1)); 
     } 
     reader.close(); 
    } 
} 

此外,我與Reimeus同意,它可能會使用分析工具或庫,而不是試圖爲HTML使用正則表達式模式解析更好的策略。但我想只要你知道頁面的結構並確切知道你想要什麼,像你或我的快速解決方案就沒關係。