gpt4 book ai didi

Java : Trying to find out a string in a line after a particular string and few characters

转载 作者:行者123 更新时间:2023-12-02 07:31:13 24 4
gpt4 key购买 nike

我有一个日志文件,其中包含如下行。

GET /common/data/register/ HTTP/1.1" 200 254614 "https://www.test.com/hiii/ "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.8) Gecko/20100101 Firefox/

我正在尝试获取上面行中的 http 状态代码 200。它将放置在字符串 HTTP/1.1" 之后。http 版本(这里是 1.1)可能并不在所有线路中都是通用的。

请帮我找出每一行中的http状态代码。

最佳答案

尝试这个正则表达式:-

    String str = "GET /common/data/register/ HTTP/1.1\" 200 254614 " +
"https://www.test.com/hiii/ Mozilla/5.0 " +
"(Windows NT 6.1; WOW64; rv:10.0.8) " +
"Gecko/20100101 Firefox/ ";

Pattern pattern = Pattern.compile("HTTP/\\d.\\d\"\\s(\\d{3})");
Matcher match = pattern.matcher(str);

while (match.find()) {
System.out.println(match.group(1)); // Prints 200
}

以下是上述正则表达式的解释:-

  • HTTP/\\d.\\d\" -> 将匹配 HTTP/1.1"。您可以有任何状态(例如:5.4)

  • \\d.\\d -> 匹配由点分隔的两个数字。

  • (\\d{3}) -> 连续匹配 3 位数字以匹配状态代码。我们已将其捕获到一个组中,因为我们需要此信息。

关于Java : Trying to find out a string in a line after a particular string and few characters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12893337/

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