gpt4 book ai didi

使用模式和匹配器的 Java 正则表达式

转载 作者:行者123 更新时间:2023-11-29 05:01:37 25 4
gpt4 key购买 nike

我正在尝试使用匹配器作为表达式来在我的字符串列表中查找时间戳。 ex("[00:00:00.000] ")时间戳前后有空格

我在线检查了我的正则表达式,它说它是正确的,但不适用于我的 java。它只是返回 false。

String word = " [00:00:00.000] ";
Pattern pattern = Pattern.compile("^\\s[[0-9:.]*]\\s");
Matcher matcher = pattern.matcher(word);
if(matcher.matches()){
//Do Stuff
else
//Do other Stuff

最佳答案

\\s*\\[[0-9:.]*\\]\\s*

使用这个。你不需要^escape []。看演示。

https://regex101.com/r/eX9gK2/11

如果你想要时间戳使用

\\s*\\[([0-9:.]*)\\]\\s*

并捕获group 1

关于使用模式和匹配器的 Java 正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31950152/

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