gpt4 book ai didi

java - 忽略 token 正则表达式中的字符串

转载 作者:行者123 更新时间:2023-12-02 04:09:14 26 4
gpt4 key购买 nike

我正在尝试解析日志文件,但很难尝试忽略 token 中的字符串。

我试图解析的日志的一部分:

[Wed Mar 06 20:56:27.121877 2019]

我想为日期创建一个 token ,它应该忽略第二个之后直到年份的任何值,如下所示:

Mar 06 20:56:27 2019

我的正则表达式字符串现在看起来像这样:

\[\S+ (\S+ \d+ \d+:\d+:\d+)

最佳答案

你可以这样做:

public class MyClass {
public static void main(String args[]) {
String test = "[Wed Mar 06 20:56:27.121877 2019]";
String[] arr = test.split(" ");
System.out.println(arr[1]+" "+arr[2]+" "+arr[3].substring(0,8)+" "+arr[4].substring(0,4));
}
}

输出:

Mar 06 20:56:27 2019

关于java - 忽略 token 正则表达式中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56699147/

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