我需要从日志中过滤数据,并且我需要过滤数据,该数据包含时间 13:00-14:59。 但这个和许多其他诱惑都失败了。什么也没有显示 Log file
我的方法:
public static void Proccesing(File file){
String formula = ".*1(3 [0-5][0-9]|4 [0-5][0-9]).*";
try{
BufferedReader rd= new BufferedReader(new FileReader(file));
String line = rd.readLine();
Pattern pattern = Pattern.compile(formula);
Matcher matcher = pattern.matcher(line);
while(line != null){
matcher.reset(line);
if(matcher.find()){
line = rd.readLine();
System.out.println(line);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
最佳答案
您可以使用 Java8 方法过滤日志:
public static void process(String logFile) throws IOException {
Files.lines(Path.of(logFile))
.filter(s -> s.matches(".*1(3 [0-5][0-9]|4 [0-5][0-9]).*"))
.forEach(System.out::println);
}
关于java - 通过正则表达式过滤日志文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61417888/