我需要在java中解析一个日志文件。日志文件包含有关时间处理和事件的信息。我感兴趣的行以及其中的信息在日志行条目示例中以粗体标记。对于以下内容,我需要提取处理时间和 eventID(200):
15:04:53相机192.168.1.112 - >第2帧:处理时间13.000000ms 15:04:53 - >第2帧:相机192.168.1.111:newImage(T)errorID(0) eventID(200) noOfBoundingBox(0),bbinfo(),noCentroid(0),centroidInfo(0,0,0%; /),imageReturned(F)
16:04:53相机192.168.1.111 - >第2帧:处理时间 14.000000ms 16:04:53 - >第2帧:相机192.168.1.111:newImage(T)errorID(0) eventID(201) noOfBoundingBox(0),bbinfo(),noCentroid(0) ,centroidInfo(0,0,0%; /),imageReturned(F)
我在创建正则表达式模式时遇到问题,如何在日志文件中获取特定IP的处理时间和事件ID?
答案 0 :(得分:1)
您可以通过以下代码提取它。
public static void stackOverFlowRegex(){
String text = "15:04:53 Camera 192.168.1.112 -> Frame 2: Processing Time 13.000000ms 15:04:53 -> Frame 2 : Camera 192.168.1.111: newImage(T) errorID(0) eventID(200) noOfBoundingBox(0),bbinfo(),noCentroid(0) , centroidInfo(0,0,0%;/), imageReturned(F) 16:04:53 Camera 192.168.1.111 -> Frame 2: Processing Time 14.000000ms 16:04:53 -> Frame 2 : Camera 192.168.1.111: newImage(T) errorID(0) eventID(201) noOfBoundingBox(0),bbinfo(),noCentroid(0) , centroidInfo(0,0,0%;/), imageReturned(F)";
Pattern pattern = Pattern.compile("(Processing Time(.*?)ms)(.*?)(eventID\\((.*?)\\))");//(Processing Time(.*?)ms)+(eventID\\((.*?)\\))
Matcher matcher = pattern.matcher(text);
while(matcher.find()){
System.out.format("%s => %s\n", matcher.group(1), matcher.group(4));
}
}
答案 1 :(得分:0)
public class RegEx {
public static String testdata = "15:04:53 Camera 192.168.1.112 -> Frame 2: Processing Time 13.000000ms 15:04:53 -> Frame 2 : Camera 192.168.1.111: newImage(T) errorID(0) eventID(200) noOfBoundingBox(0),bbinfo(),noCentroid(0) , centroidInfo(0,0,0%;/), imageReturned(F)";
public static void main(String[] args) {
System.out.println(testdata);
String processingTime = testdata.replaceFirst(".*Processing Time ", "");
processingTime = processingTime.replaceFirst(" .*", "");
System.out.println(processingTime);
String eventId = testdata.replaceFirst(".*eventID\\(", "");
eventId = eventId.replaceFirst("\\).*", "");
System.out.println(eventId);
}
}