我正在尝试使用匹配器作为表达式来在我的字符串列表中查找时间戳。 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/