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

huangapple go评论94阅读模式
英文:

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();
}

但是我无法使它工作。我错在哪里?基本上,我需要为每个字符串选择相应的值(最小值和最大值),这些值来自于那段长文本,并将它们存储到变量中。就像这样:

FPSMin = 29.0000
FPSMax = 35.0000
FramesMin = 100000
等等

谢谢

编辑:
我尝试了以下代码(在一个测试案例中)来查看解决方案是否可行,但是我遇到问题,因为我除了一个对象之外什么都不能打印出来。这是代码:

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

    String[] FPSMin = new String[0];
    String FPSMax = "";

    // 逐行读取文件
    while (txtScan.hasNextLine()) {
        // 将内容打印到控制台
        String str = txtScan.nextLine();
        Pattern FPSMinPattern = Pattern.compile("^FPS\\(FramesPerSecond\\)\\[ValMin:");
        Matcher matcher = FPSMinPattern.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);
    }
}
英文:

I have a very long text and I'm extracting some specific values that are followed by some particular words. Here's an example of my long text:

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

here's my code:

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();
        }

But I can't make it work. Where am I wrong? Basically I need to select, for each String, the corresponding values (max and min) coming from that long text and store them into the variables. Like

FPSMin = 29.0000
FPSMax = 35.0000
FramesMin = 100000
Etc 

Thank you

EDIT:
I tried the following code (in a test case) to see if the solution could work, but I'm experiencing issues because I can't print anything except an object. Here's the code:

 @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);

        }

答案1

得分: 0

或许你的模式应该像这样:^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的所有字符(因为你不想获取]字符)。

英文:

Maybe your pattern should be like this ^FPS\\(FramesPerSecond\\)\\[ValMin: . I've tried it and it works for me.

	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));
		
	}
}

In that way, you get the offset of the line that you want to extract data and using the substring function you can get all characters starting from offset until the size of the line-1 (because you dont want to get also the ] character)

答案2

得分: 0

以下是翻译好的部分:

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

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

用法(提取捕获的分组):

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 <br>
> TotalFrames, 100000, 200000 <br>
> MemoryUsage(In MB), 190000, 360000
英文:

The following regular expression will match and capture the name, min and max:

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

Usage (extracting the captured groups):

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

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

Output:

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

huangapple
  • 本文由 发表于 2020年10月27日 16:36:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/64550698.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定