I need to implement
File[] files = getFiles( String folderName, String ptrn );
Where ptrn is a command prompt style pattern like "*2010*.txt"
I'm familar with FilenameFilter class, but can't implement
public boolean accept(File dir, String filename)
because String.matches() doesn't accept such patterns.
Thanks!
Pattern matching has modified two syntactic elements of the Java language: the instanceof keyword and switch statements. They were both extended with a special kind of patterns called type patterns. There is more to come in the near future.
Interface FilenameFilterInstances of classes that implement this interface are used to filter filenames. These instances are used to filter directory listings in the list method of class File , and by the Abstract Window Toolkit's file dialog component.
The matcher() method of this class accepts an object of the CharSequence class representing the input string and, returns a Matcher object which matches the given string to the regular expression represented by the current (Pattern) object.
Solution: Use the Java Pattern and Matcher classes, supply a regular expression (regex) to the Pattern class, use the find method of the Matcher class to see if there is a match, then use the group method to extract the actual group of characters from the String that matches your regular expression.
The String#matches()
accepts regular expression patterns.
The regex variant of the "layman's" variant *2010*.txt
would be .*2010.*\.txt
.
So the following should work:
public boolean accept(File dir, String name) {
return name.matches(".*2010.*\\.txt");
}
The double backslash is just there to represent an actual backslash because the backslash itself is an escape character in Java's String
.
Alternatively, you can also do it without regex using the other String
methods:
public boolean accept(File dir, String name) {
return name.contains("2010") && name.endsWith(".txt");
}
Your best bet is likely to let ptrn
represent a real regex pattern or to string-replace every .
with \.
and *
with .*
so that it becomes a valid regex pattern.
public boolean accept(File dir, String name) {
return name.matches(ptrn.replace(".", "\\.").replace("*", ".*"));
}
You may need to scape your specific wild cards for those used in Java regex.
For instance to replace "*" you could use something like:
import java.io.*;
class Filter {
public static void main ( String [] args ) {
String argPattern = args[0];
final String pattern = argPattern.replace(".","\\.").replace("*",".*");
System.out.println("transformed pattern = " + pattern );
for( File f : new File(".").listFiles( new FilenameFilter(){
public boolean accept( File dir, String name ) {
return name.matches( pattern );
}
})){
System.out.println( f.getName() );
}
}
}
$ls -l *ter.*
-rw-r--r-- 1 oscarreyes staff 1083 Jun 16 17:55 Filter.class
-rw-r--r-- 1 oscarreyes staff 616 Jun 16 17:56 Filter.java
$java Filter "*ter.*"
transformed pattern = .*ter\..*
Filter.class
Filter.java
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With