gpt4 book ai didi

java - 如何在正则表达式匹配后跳过 header 并使用 BufferedReader 读取下一行

转载 作者:行者123 更新时间:2023-12-01 09:40:59 26 4
gpt4 key购买 nike

我有一个流,它继续打印行,如下所示:

11:19:54 AM     CPU     %user     %nice   %system   %iowait    %steal     %idle
11:19:55 AM all 0.00 0.00 1.00 0.00 0.00 99.00
Average: all 0.00 0.00 1.00 0.00 0.00 99.00

11:19:55 AM CPU %user %nice %system %iowait %steal %idle
11:19:56 AM all 0.00 0.00 1.00 0.00 0.00 99.00
Average: all 0.00 0.00 1.00 0.00 0.00 99.00

...等等

Java 代码是:

    public final static String REGEX_STARTS_WITH_TIME = "[0-9]{2}:[0-9]{2}:[0-9]{2}\\s+[A-Z]{2}\\s";
public final static String REGEX = REGEX_STARTS_WITH_TIME + "CPU\\s+%usr\\s";
String line;
try {
InputStream is = process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));

while ((line = br.readLine()) != null) {
line.matches(REGEX);
//Here I want to read the next line by skipping the header.
}
br.close();
} catch (IOException e) {
......
}

我想跳过标题并只读取下一行。

最佳答案

您可以使用string#contains("CPU")来检查该行是否包含该子字符串。如果是,(那么它是一个 header )只需在循环中调用 continue 即可。

旁注:这也可以使用 indexOf() 来实现。我看不出有任何理由在这里使用正则表达式。

关于java - 如何在正则表达式匹配后跳过 header 并使用 BufferedReader 读取下一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38457073/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com