2017-07-05 24 views
0

我想弄清楚如何使用正則表達式來壓縮和排序我從這段代碼中獲得的信息。下面的代碼,我會解釋一下,我去:從文本日誌文件顯示消息的正則表達式

import java.io.*; 
import java.util.*; 

public class baseline 
{ 

// Class level variables 
    static Scanner sc = new Scanner(System.in); 

    public static void main(String[] args) throws IOException, 
    FileNotFoundException { // Start of main 

    // Variables 
    String filename; 

    // Connecting to the output file with a buffer 
    PrintWriter outFile = new PrintWriter(
          new BufferedWriter(
          new FileWriter("chatOutput.log"))); 

    // Get the input file 
    System.out.print("Please enter full name of the file: "); 
    filename = sc.next(); 

    // Assign the name of the input file to a file object 
    File log = new File(filename); 
    String textLine = null; // Null 
    String outLine = ""; // Null 
    BufferedWriter bw = null; 



    try 
    { 
    // assigns the input file to a filereader object 
    BufferedReader infile = new BufferedReader(new FileReader(log)); 

     sc = new Scanner(log); 
      while(sc.hasNext()) 
      { 
       String line=sc.nextLine(); 
       if(line.contains("LANTALK")) 
        System.out.println(line); 
      } // End of while 


    try 
    { 
    // Read data from the input file 
    while((textLine = infile.readLine()) != null) 
    { 
    // Print to output file 
    outLine = textLine; 
    sc = new Scanner (outLine); 
      while(sc.hasNext()) 
      { 
       String line=sc.nextLine(); 
       if(line.contains("LANTALK")) 
        outFile.printf("%s\n",outLine); 
      }// end of while 
     } // end of while 
    } // end of try 


    finally // This gets executed even when an exception is thrown 
     { 
    infile.close(); 
    outFile.close(); 
     } // End of finally 
    } // End of try 


    catch (FileNotFoundException nf) // Goes with first try 
    { 
    System.out.println("The file \""+log+"\" was not found"); 
    } // End of catch 
    catch (IOException ioex) // Goes with second try 
    { 
    System.out.println("Error reading the file"); 
    } // End of catch 

} // end of main 

} // end of class 

所以我讀一個輸入文件,只得到這顯示「LANTALK」行,並打印出來到另一個文件。這裏是輸出的樣子至今的樣本:

14:29:39.731 [D] [T:000FEC] [F:LANTALK2C] <CMD>LANMSG</CMD> 
<MBXID>922</MBXID><MBXTO>5608</MBXTO><SUBTEXT>LanTalk</SUBTEXT><MOBILEADDR> 
</MOBILEADDR><LAP>0</LAP><SMS>0</SMS><MSGTEXT>It is mailing today right? 
</MSGTEXT> 
14:41:33.703 [D] [T:000FF4] [F:LANTALK2C] <CMD>LANMSG</CMD> 
<MBXID>929</MBXID><MBXTO>5601</MBXTO><SUBTEXT>LanTalk</SUBTEXT><MOBILEADDR> 
</MOBILEADDR><LAP>0</LAP><SMS>0</SMS><MSGTEXT>Either today or tomorrow - 
still waiting to hear. </MSGTEXT> 

而我需要的是讓所有<MSGTEXT></MSGTEXT>之間的字符,以便能夠清晰地顯示信息。我應該如何將這些代碼寫入代碼,以便每個「LANTALK」行重複並仍然正確寫出?謝謝!

+2

你在這裏什麼是XML結構,我嚴重懷疑會有一個正確的正則表達式提取你想要的東西 - 你會好起來的解析東西XML和看數據然後。 –

+0

'grep LANTALK chatOutput.log' – gnomed

+0

僅作爲示例,請考慮以下行:' MetaTalk那麼,我們將如何解決LanTalk頻道的問題?' –

回答

0

試用Jsoup

例子:

import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 
import org.jsoup.nodes.Element; 


.... 

while(sc.hasNext()) 
     { 
      String line=sc.nextLine(); 
      if(line.contains("LANTALK")){ 
       Document doc = Jsoup.parse(line); 
       Element msg = doc.select("MSGTEXT").first(); 
       System.out.println(msg.text()); 
      } 
       System.out.println(line); 
     } // End of while 
    ..... 
+0

好吧,我正在嘗試這個解決方案,但它給了我一個非法的表達錯誤開始,而我不明白爲什麼。我所有的括號都是他們應該在的地方。 –

+0

檢查你是否在你的條件之後放置了固定支架。在你的代碼中,如果你有一個單獨的語句,那麼沒有大括號是可以的,但是如果你想在if塊中分組多個語句,那麼你必須使用大括號。 – Eritrean

0

可以使用正則表達式查找MSGTEXT

<MSGTEXT>(.*?)</MSGTEXT> 

然而,一些郵件包含換行符,這使得這個有點難度。

克服這個問題的一種方法是將整個文件讀入字符串,然後查找匹配項。

try { 
    String text = new String(Files.readAllBytes(Paths.get(log))); 
    Matcher m = Pattern.compile("<MSGTEXT>(.*?)</MSGTEXT>", Pattern.DOTALL).matcher(text); 
    while (m.find()) { 
     System.out.println("Message: " + m.group(1)); 
    } 
} catch (IOException e) { 
    //Handle exception 
} 

控制檯輸出:

Message: It is mailing today right? 

Message: Either today or tomorrow - 
still waiting to hear. 

請記住,如果你正在處理大量的日誌文件這種方法可以使用大量的內存。

另請注意,用正則表達式解析XML通常被認爲是一個壞主意;它現在可以正常工作,但是如果你打算做更復雜的事情,你應該像其他人一樣使用XML解析器。

+0

除了.io和.util之外,我還需要導入任何東西嗎? –

+0

@AlexanderVassios您將需要'java.io'中的'IOException','java.nio.file'中的'Files'和'Paths'以及'java.util.regex'中的'Matcher'和'Pattern'。 – InternetUnexplorer

+0

啊啊,謝謝! –

相關問題