我试图匹配名称中有两个点的目录中的文件,就像theme.default.properties
我认为模式.\\..\\..应该是所需的模式[ .匹配任何字符并\.匹配一个dot]但它匹配两者oneTwo.txt和theme.default.properties
我尝试了以下内容:
[ resources/themes有两个文件oneTwo.txt,theme.default.properties]
1.
public static void loadThemes()
{
File themeDirectory = new File("resources/themes");
if(themeDirectory.exists())
{
File[] themeFiles = themeDirectory.listFiles();
for(File themeFile : themeFiles)
{
if(themeFile.getName().matches(".\\..\\.."))
{
System.out.println(themeFile.getName());
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这没什么打印
和以下
File[] themeFiles = themeDirectory.listFiles(new FilenameFilter()
{
public boolean accept(File dir, String name)
{
return name.matches(".\\..\\..");
}
});
for (File file : themeFiles)
{
System.out.println(file.getName());
}
Run Code Online (Sandbox Code Playgroud)
打印两者
oneTwo.txt
theme.default.properties
Run Code Online (Sandbox Code Playgroud)
我无法找到为什么这两个给出不同的结果以及我应该使用哪种模式来匹配两个点......
有人可以帮忙吗?
如果文件名的名称中有两个点,用单词字符分隔,则返回true:
matches("\\w+\\.\\w+\\.\\w+")
Run Code Online (Sandbox Code Playgroud)
匹配以下内容:
aaa.bbb.ccc
aaa.bbb.ccc
111.aaa.bbb
aaa.b_b.ccc
a.b.c
Run Code Online (Sandbox Code Playgroud)
与以下内容不符:
aaa.bbb
..
.
---.aaa.bbb
aaa.bbb.ccc.ddd
a-a.bbb.ccc
Run Code Online (Sandbox Code Playgroud)