Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java file extension regex

Tags:

java

regex

I'm trying to come up with a Java regex that will match a filename only if it has a valid extension. For example it should match "foo.bar" and "foo.b", but neither "foo." nor "foo".

I've written the following test program

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);
}

I expect this to print "true true false false", but instead it prints false for all 4 cases. Where am I going wrong?

Cheers, Don

like image 796
Dónal Avatar asked Dec 03 '22 09:12

Dónal


2 Answers

The Matcher.matches() function tries to match the pattern against the entire input. Thus, you have to add .* to the beginning of your regex (and the \\Z at the end is superfluous, too), or use the find() method.

like image 81
Adam Rosenfield Avatar answered Dec 17 '22 18:12

Adam Rosenfield


public boolean isFilename(String filename) {
    int i=filename.lastInstanceOf(".");
    return(i != -1 && i != filename.length - 1)
}

Would be significantly faster and regardless of what you do, putting it in a method would be more readable.

like image 33
Bill K Avatar answered Dec 17 '22 18:12

Bill K