List files with two dots in their names using java regular expressions
Posted
by Nivas
on Stack Overflow
See other posts from Stack Overflow
or by Nivas
Published on 2010-04-15T16:53:22Z
Indexed on
2010/04/15
17:13 UTC
Read the original article
Hit count: 247
I was trying to match files in a directory that had two dots in their name, something like theme.default.properties
I thought the pattern .\\..\\..
should be the required pattern [.
matches any character and \.
matches a dot
] but it matches both oneTwo.txt
and theme.default.properties
I tried the following:
[resources/themes
has two files oneTwo.txt
and 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());
}
}
}
}
This prints nothing
and the following
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());
}
prints both
oneTwo.txt
theme.default.properties
I am unable to find why these two give different results and which pattern I should be using to match two dots...
Can someone help?
© Stack Overflow or respective owner