2011-07-24 6 views
0

私はこのような何かで区切られたメッセージの50メガを含むメールボックスファイルを持っている:正規表現

から - 木7月19日午前7時11分55秒2007

私が構築したいです定期的な時に各メールメッセージ1を抽出するためにJavaで、このための表現、私は区切り文字として、次のパターンを使用して、スキャナを使用してみました:

public boolean ParseData(DataSource data_source) { 

    boolean is_successful_transfer = false; 
    String mail_header_regex = "^From\\s"; 
    LinkedList<String> ip_addresses = new LinkedList<String>(); 
    ASNRepository asn_repository = new ASNRepository(); 

    try {  

    Pattern mail_header_pattern = Pattern.compile(mail_header_regex); 

    File input_file = data_source.GetInputFile(); 

    //parse out each message from the mailbox 
    Scanner scanner = new Scanner(input_file);  

    while(scanner.hasNext(mail_header_pattern)) { 


    String current_line = scanner.next(mail_header_pattern); 

    Matcher mail_matcher = mail_header_pattern.matcher(current_line); 

     //read each mail message and extract the proper "received from" ip address 
     //to put it in our list of ip's we can add to the database to prepare 
     //for querying. 
     while(mail_matcher.find()) { 
      String message_text = mail_matcher.group();     
      String ip_address = get_ip_address(message_text); 

      //empty ip address means the line contains no received from 
      if(!ip_address.trim().isEmpty()) 
       ip_addresses.add(ip_address); 
     } 

    }//next line 

     //add ip addresses from mailbox to database 
     is_successful_transfer = asn_repository.AddIPAddresses(ip_addresses);   

    } 

    //error reading file--unsuccessful transfer 
    catch(FileNotFoundException ex) { 
     is_successful_transfer = false; 
    } 

    return is_successful_transfer; 

} 

それが動作するはずのようにこれは思えるが、私はそれを実行するたびにおそらくパターンが見つからないために、プログラムがハングします。同じ正規表現は同じファイルでPerlで動作しますが、Javaでは常にこのファイルがハングしますString current_line = scanner.next(mail_header_pattern);

この正規表現は正しいですか、またはファイルを正しく解析していませんか?

答えて

1

私はちょうどライン、このような何か読んで、単純ずっと何かに傾いているはずだ:のstring.Split()(とは改行で^交換し、それを終了するものの、

while(scanner.hasNextLine()) { 
    String line = scanner.nextLine(); 
    if (line.matches("^From\\s.*")) { 
     // it's a new email 
    } else { 
     // it's still part of the email body 
    } 
} 
+0

うんを改行を含む)はもっと簡単な解決策になるかもしれません。 – Voo