Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 涉及YYYYMMDD的文件名不起作用的模式_Java_Regex - Fatal编程技术网

Java 涉及YYYYMMDD的文件名不起作用的模式

Java 涉及YYYYMMDD的文件名不起作用的模式,java,regex,Java,Regex,我正在从远程位置获取文件FTPFile。 我想将它的名称与前面提到的正则表达式模式匹配 Regex pattern: hnm_sarai_ntpl_((19|20)\\d\\d)(0?[1-9]|1[012])(0?[1-9]|[12][0-9]|3[01]) File name: hnm_sarai_ntpl_20130808 List fileNamePatterns=new ArrayList(); 添加(“hnm|u sarai|u ntpl|((19 | 20)\\d\\d)(0?[

我正在从远程位置获取文件FTPFile。 我想将它的名称与前面提到的正则表达式模式匹配

Regex pattern: hnm_sarai_ntpl_((19|20)\\d\\d)(0?[1-9]|1[012])(0?[1-9]|[12][0-9]|3[01])
File name: hnm_sarai_ntpl_20130808
List fileNamePatterns=new ArrayList();
添加(“hnm|u sarai|u ntpl|((19 | 20)\\d\\d)(0?[1-9]| 1[012])(0?[1-9]|[12][0-9]| 3[01]);
FTPFile文件//文件名为=hnm\u sarai\u ntpl\u 20130808
Pattern p=Pattern.compile(_fileNamePatterns.get(0));
Matcher m=p.Matcher(file.getName());
如果(m.matches()){
返回文件.getName();
}否则{
返回“”;
}
代码段总是返回
,我希望文件名应该与给定的正则表达式匹配。
知道为什么不匹配吗?

为什么不使用正则表达式来解析实际日期呢。这可以避免在没有闰日的年份中使用闰日。等等。或者,如果你不需要日期作为日期,只需查找
\\d{8,}
好的,remore目录在过去5天内至少可以有5个文件,我对获取所有这5个文件感兴趣,因此我无法解析实际日期。
模式.compile(_fileNamePatterns.get(0))中的下划线是什么应该是什么意思?请将您的示例中的问题缩小为一个完整的、可运行的程序,以再现该问题。例如,添加一个print语句以获取字符串
file.getName()
返回,创建一个基本上由
thatString.matches(yourRegex)组成的新程序,然后给我们这个程序(或者更可能的情况是你自己解决你的问题)。
List<String> fileNamePatterns=new ArrayList<String>();
fileNamePatterns.add("hnm_sarai_ntpl_((19|20)\\d\\d)(0?[1-9]|1[012])(0?[1-9]|[12][0-9]|3[01])");

FTPFile file; //file's name is = hnm_sarai_ntpl_20130808

Pattern p = Pattern.compile(_fileNamePatterns.get(0));
Matcher m = p.matcher(file.getName());
if (m.matches()) {
    return file.getName();
} else {
    return "";
}