我试图想出一个Java正则表达式,它只会匹配一个文件名,如果它有一个有效的扩展名。例如它应该匹配“foo.bar”和“foo.b”,但不是“foo”。也不是“富”。Java文件扩展名正则表达式
我已经写了下面的测试程序
public static void main(String[] args) {
Pattern fileExtensionPattern = Pattern.compile("\\.\\w+\\z");
boolean one = fileExtensionPattern.matcher("foo.bar").matches();
boolean two = fileExtensionPattern.matcher("foo.b").matches();
boolean three = fileExtensionPattern.matcher("foo.").matches();
boolean four = fileExtensionPattern.matcher("foo").matches();
System.out.println(one + " " + two + " " + three + " " + four);
}
我预计这将打印“真真的虚假”,而是它打印所有4个伪。我哪里错了?
干杯, 唐
刚刚昨天就打我了。 – agnul 2008-10-24 16:55:35
此外,唐可能会考虑在FileFilter中包装他的正则表达式: http://java.sun.com/j2se/1.4.2/docs/api/java/io/FileFilter.html – 2008-10-25 04:10:08