2014-01-06 26 views
0

我想解析一個文本文件,我已經轉換爲字符串了。 我試圖解析的文本(每次生成時都會更改),但始終有一個或多個部分(錯誤,警告,合規性,指導性)這些行以「錯誤」,「警告」,「合規性」或「教學」。用Java解析日誌文件到打印行

這裏是一個文本例如:

Errors - This section contains errors 
The cake is a lie 
All things have endings 
Twinkies are back 
Warnings - This contains warnings 
Show me the money 
Metric > Imperial 
food for thought 
derp derp derp 
Compliance- This contains compliance issues 
Space is disease and danger wrapped in darkness and silence. 
Khaaaaaaaaaaan! 
Instructional - Contains Instructional Issues 
I'm a doctor, not an escalator. 

我需要測試每行

Psudo代碼:

boolean E = false; 
boolean W = false; 
boolean C = false; 
boolean I = false; 
boolean Skip5 = false; 

For each line in the stringFromTextFile{ 
Skip5 = false; 
if the line starts with "Errors" 
    E = true; 
    W = false; 
    C = false; 
    I = false; 
    Skip5 = true;//start over with next line 
if the line starts with "Warnings" 
    E = false; 
    W = true; 
    C = false; 
    I = false; 
    Skip5 = true;//start over with next line 
if the line starts with "Compliance" 
    E = false; 
    W = false; 
    C = true; 
    I = false; 
    Skip5 = true;//start over with next line 
if the line starts with "Instructional" 
    E = false; 
    W = false; 
    C = false; 
    I = true; 
    Skip5 = true;//start over with next line 

//Step 5 
if(!Skip5){ 
if(I) 
    println "Instructional " + currentLine; 
if(E) 
    println "Error " + currentLine; 
if(W) 
    println "Warning " + currentLine; 
if(C) 
    println "Compliance " + currentLine; 
} 
//End Step 5 
}//end for each 

結果的例子,我試着從上面的文字可以得到:

Error The cake is a lie 
Error All things have endings 
Error Twinkies are back 
Warning Show me the money 
Warning Metric > Imperial 
Warning food for thought 
Warning derp derp derp 
Compliance Space is disease and danger wrapped in darkness and silence. 
Compliance Khaaaaaaaaaaan! 
Instructional I'm a doctor, not an escalator. 

感謝您的幫助!請讓我知道如果我不明確尋找什麼。我沒有太多的解析字符串和任何幫助我可以得到的經驗。

+0

你嘗試過正則表達式嗎? – tuxdna

+0

我在reg ex中遇到的問題是,每行不以「錯誤」爲例。它交替。你能否給我看一個你想到的例子? – user1857654

+0

所以你可以簡單地使用正則表達式:^((Error | Warning | Compliance)。+)你可以放任何你想查找的單詞 – Darka

回答

0
String parsedText = output.toString(); 

     boolean E = false; 
     boolean W = false; 
     boolean C = false; 
     boolean I = false; 
     boolean Skip5 = false; 
     Scanner scanner = new Scanner(parsedText); 
     while (scanner.hasNextLine()) { 
      String line = scanner.nextLine(); 
      Skip5 = false; 
      if (line.startsWith("Errors")){ 
       E = true; 
       W = false; 
       C = false; 
       I = false; 
       Skip5 = true;//start over with next line 
      } 
      if(line.startsWith("Warnings")){ 
       E = false; 
       W = true; 
       C = false; 
       I = false; 
       Skip5 = true;//start over with next line 
      } 
      if(line.startsWith("Compliance")){ 
       E = false; 
       W = false; 
       C = true; 
       I = false; 
       Skip5 = true;//start over with next line 
      } 
      if(line.startsWith("Instructional")){ 
       E = false; 
       W = false; 
       C = false; 
       I = true; 
       Skip5 = true;//start over with next line 
      } 
      //Step 5 
      if(!Skip5){ 
       if(I){ 
        System.out.println("Instructional " + line.toString()); 
       } 
       if(E){ 
        System.out.println("Error " + line.toString()); 
       } 
       if(W){ 
        System.out.println("Warning " + line.toString()); 
       } 
       if(C){ 
        System.out.println("Compliance " + line.toString()); 
       } 
      } 


     } 
0

這裏是一個Ruby版本,你可以很容易地適應到Java:

current_prefix = "" 

ARGF.each do |line| 
    regex = /^(Errors|Warnings|Compliance|Instructional)\s*-/ 
    m = regex.match(line) 
    if m then 
    new_prefix = case m[1] 
       when 'Errors' then 'Error' 
       when 'Warnings' then 'Warning' 
       when 'Compliance' then 'Compliance' 
       when 'Instructional' then 'Instructional' 
       end 
    current_prefix = new_prefix 
    else 
    puts "#{current_prefix} - #{line}" 
    end 
end 

樣品調用

$ ruby myconfig-parser.rb sample.txt 
Error - The cake is a lie 
Error - All things have endings 
Error - Twinkies are back 
Warning - Show me the money 
Warning - Metric > Imperial 
Warning - food for thought 
Warning - derp derp derp 
Compliance - Space is disease and danger wrapped in darkness and silence. 
Compliance - Khaaaaaaaaaaan! 
Instructional - I'm a doctor, not an escalator. 

我希望幫助。