我将尝试编写一个Java正则表达式,只有当文件名具有有效扩展名时才匹配。例如,它应该匹配"foo.bar"和"foo.b",但不匹配"foo."或"foo"。
我已经编写了以下测试程序。
我希望这段代码输出"true true false false",但实际上它对于所有4个情况都输出了false。我哪里出错了呢?
谢谢, Don
我已经编写了以下测试程序。
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);
}
我希望这段代码输出"true true false false",但实际上它对于所有4个情况都输出了false。我哪里出错了呢?
谢谢, Don