使用Java将日志文件解析为打印行
我正在尝试解析一个已转换为字符串的文本文件。 我试图解析的文本(每次生成时都会更改),但总是有一个或多个部分(错误、警告、合规性、说明性)这些行以“错误”、“警告”、“合规性”或“说明性”开头 以下是一个文本示例:使用Java将日志文件解析为打印行,java,regex,parsing,Java,Regex,Parsing,我正在尝试解析一个已转换为字符串的文本文件。 我试图解析的文本(每次生成时都会更改),但总是有一个或多个部分(错误、警告、合规性、说明性)这些行以“错误”、“警告”、“合规性”或“说明性”开头 以下是一个文本示例: Errors - This section contains errors The cake is a lie All things have endings Twinkies are back Warnings - This contains warnings Show me t
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.
谢谢你的帮助!请让我知道,如果我在找什么不清楚。我在解析字符串方面没有太多经验,也没有得到任何帮助 这是一个Ruby版本,您可以轻松地适应Java:
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());
}
}
}
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.
我希望这会有所帮助。您尝试过正则表达式吗?我使用reg-ex时遇到的问题是,每一行都不是以例如“Errors”开头的。它交替出现。你能给我举个例子说明你的想法吗?所以你可以简单地用正则表达式:^((Error | Warning | Compliance)。+)你可以放任何你想找的词。问题是每个词都会产生不同的结果,就像上面的示例文本一样。明白了。张贴我所做的事情