使用Java将日志文件解析为打印行

时间:2014-01-06 14:07:55

标签: 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 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.

谢谢你的帮助!如果我正在寻找的不清楚,请告诉我。我没有太多的经验来解析字符串和我可以获得的任何帮助。

2 个答案:

答案 0 :(得分: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());
              }
          }


        }

答案 1 :(得分: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.

我希望有所帮助。