code_junkie

Java file extension regex

Nov 11th, 2011
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. public static void main(String[] args) {
  2. Pattern fileExtensionPattern = Pattern.compile("\.\w+\z");
  3.  
  4. boolean one = fileExtensionPattern.matcher("foo.bar").matches();
  5. boolean two = fileExtensionPattern.matcher("foo.b").matches();
  6. boolean three = fileExtensionPattern.matcher("foo.").matches();
  7. boolean four = fileExtensionPattern.matcher("foo").matches();
  8.  
  9. System.out.println(one + " " + two + " " + three + " " + four);
  10. }
  11.  
  12. public boolean isFilename(String filename) {
  13. int i=filename.lastInstanceOf(".");
  14. return(i != -1 && i != filename.length - 1)
  15. }
Add Comment
Please, Sign In to add comment