Java 正则表达式不匹配,正则表达式看起来正常

标签 java regex

以下内容不返回任何匹配项:

String patternStr = "((19\\d{2}|20\\d{2})-([0-2]\\d{2}|3[0-5]\\d)-(([0-1]\\d|2[0-3])[0-5]\\d[0-5]\\d))";
String fullPath = aFile.getAbsolutePath(); 
// fullPath should expand to this: "/home/user1/2013-023-135159_abcd_001/File.txt"

Pattern p = Pattern.compile(patternStr);
Matcher m = p.matcher(fullPath);
if (m.matches())
{
   System.out.println("Matches found");
}

它应该与日期部分匹配,2013-023-135159。我在线测试了它,正则表达式看起来没问题。

最佳答案

您将需要使用:

m.find()

而不是:

m.matches()

由于您的正则表达式与输入字符串的部分匹配不完全符合 m.matches()

RegEx Demo

关于Java 正则表达式不匹配,正则表达式看起来正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29091487/

相关文章:

javax.naming.NoInitialContextException 与 mysql 数据源

java - 使用静态列表保留Java中的所有线程

mysql - 更新 MySQL(使用正则表达式?)

javascript 正则表达式 unicode 帮助

java - replaceFirst 不起作用,但 replace 在完全相同的输入上起作用?

c++ - C++ 正则表达式中的字符类无法正常工作

mysql:最好的说法是 "the string up to the first instance of any of the following keywords"?

java - 我如何计算 word 中常量的索引并在 java 中对其求和?

java - 谷歌 Collection 中是否有不区分大小写的多图

java - 在方法中实现计时器