Java 正则表达式提取长日志中的特定值

Java regex extract specific values in long log

我有一个很长的文本,我正在提取一些特定的值,然后是一些特定的词。这是我的长文本示例:

.........
FPS(FramesPerSecond)[ValMin: 29.0000, ValMax: 35.000]
.........
TotalFrames[ValMin: 100000, ValMax:200000]
.........
MemoryUsage(In MB)[ValMin:190000MB, ValMax:360000MB]
.........

这是我的代码:

File file = filePath.toFile();
        JSONObject jsonObject = new JSONObject();
String FPSMin="";
String FPSMax="";
String TotalFramesMin="";
String TotalFramesMax="";
String MemUsageMin="";
String MemUsageMax="";

String log = "my//log//file";

        final Matcher matcher = Pattern.compile("FPS/\(FramesPerSecond/\)/\[ValMin:");
        if(matcher.find()){
            FPSMin= matcher.end().trim();
        }

但我做不到。我哪里错了?基本上我需要 select,对于每个字符串,相应的值(最大值和最小值)来自那个长文本并将它们存储到变量中。喜欢

FPSMin = 29.0000
FPSMax = 35.0000
FramesMin = 100000
Etc 

谢谢

编辑: 我尝试了以下代码(在测试用例中)以查看解决方案是否可行,但我遇到了问题,因为我无法打印除对象之外的任何内容。这是代码:

 @Test
    public void whenReadLargeFileJava7_thenCorrect()
            throws IOException, URISyntaxException {
        Scanner txtScan = new Scanner("path//to//file//test.txt");


        String[] FPSMin= new String[0];
        String FPSMax= "";
  
//Read File Line By Line
        while (txtScan.hasNextLine())   {
            // Print the content on the console
            String str = txtScan.nextLine();
            Pattern FPSMin= Pattern.compile("^FPS\(FramesPerSecond\)\[ValMin:");
            Matcher matcher = FPSMin.matcher(str);

            if(matcher.find()){
                String MinMaxFPS= str.substring(matcher.end(), str.length()-1);
                String[] splitted = MinMaxFPS.split(",");
                FPSMin= splitted[0].split(": ");
                FPSMax = splitted[1];

            }

            System.out.println(FPSMin);
            System.out.println(FPSMax);

        }

也许你的模式应该是这样的^FPS\(FramesPerSecond\)\[ValMin:。我试过了,对我有用。

    String line = "FPS(FramesPerSecond)[ValMin: 29.0000, ValMax: 35.000]";
    
    Pattern pattern = Pattern.compile("^FPS\(FramesPerSecond\)\[ValMin:");
    
    Matcher matcher = pattern.matcher(line);
    
    if (matcher.find()) {
        
        System.out.println(line.substring(matcher.end(), line.length()-1));
        
    }
}

通过这种方式,您可以获得要提取数据的行的偏移​​量,并使用子字符串函数可以获取从偏移量开始直到第 1 行大小的所有字符(因为您不想获取还有 ] 字符)

以下正则表达式将匹配并捕获名称、最小值和最大值:

Pattern.compile("(.*)\[.+:\s*(\d+(?:\.\d+)?)[A-Z]*,.+:\s*(\d+(?:\.\d+)?)[A-Z]*\]");

用法(提取捕获的组):

String input = (".........\n" +
        "FPS(FramesPerSecond)[ValMin: 29.0000, ValMax: 35.000]\n" +
        ".........\n" +
        "TotalFrames[ValMin: 100000, ValMax:200000]\n" +
        ".........\n" +
        "MemoryUsage(In MB)[ValMin:190000MB, ValMax:360000MB]\n" +
        ".........");

for (String s : input.split("\n")) {
    Matcher matcher = pattern.matcher(s);
    if (matcher.matches()) {
        System.out.println(matcher.group(1) + ", " + matcher.group(2) + ", " + matcher.group(3));
    }
}

输出:

FPS(FramesPerSecond), 29.0000, 35.000
TotalFrames, 100000, 200000
MemoryUsage(In MB), 190000, 360000