我想解析一個文本文件,我已經轉換爲字符串了。 我試圖解析的文本(每次生成時都會更改),但始終有一個或多個部分(錯誤,警告,合規性,指導性)這些行以「錯誤」,「警告」,「合規性」或「教學」。用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.
感謝您的幫助!請讓我知道如果我不明確尋找什麼。我沒有太多的解析字符串和任何幫助我可以得到的經驗。
你嘗試過正則表達式嗎? – tuxdna
我在reg ex中遇到的問題是,每行不以「錯誤」爲例。它交替。你能否給我看一個你想到的例子? – user1857654
所以你可以簡單地使用正則表達式:^((Error | Warning | Compliance)。+)你可以放任何你想查找的單詞 – Darka