FileDocCategorySizeDatePackage
RegexFileFilter.javaAPI DocAndroid 1.5 API4208Wed May 06 22:42:46 BST 2009org.apache.commons.io.filefilter

RegexFileFilter

public class RegexFileFilter extends AbstractFileFilter implements Serializable
Filters files using supplied regular expression(s).

See java.util.regex.Pattern for regex matching rules

e.g.

File dir = new File(".");
FileFilter fileFilter = new RegexFileFilter("^.*[tT]est(-\\d+)?\\.java$");
File[] files = dir.listFiles(fileFilter);
for (int i = 0; i < files.length; i++) {
System.out.println(files[i]);
}
author
Oliver Siegmar
version
$Revision: 606381 $
since
Commons IO 1.4

Fields Summary
private final Pattern
pattern
The regular expression pattern that will be used to match filenames
Constructors Summary
public RegexFileFilter(String pattern)
Construct a new regular expression filter.

param
pattern regular string expression to match
throws
IllegalArgumentException if the pattern is null

        if (pattern == null) {
            throw new IllegalArgumentException("Pattern is missing");
        }

        this.pattern = Pattern.compile(pattern);
    
public RegexFileFilter(String pattern, IOCase caseSensitivity)
Construct a new regular expression filter with the specified flags case sensitivity.

param
pattern regular string expression to match
param
caseSensitivity how to handle case sensitivity, null means case-sensitive
throws
IllegalArgumentException if the pattern is null

        if (pattern == null) {
            throw new IllegalArgumentException("Pattern is missing");
        }
        int flags = 0;
        if (caseSensitivity != null && !caseSensitivity.isCaseSensitive()) {
            flags = Pattern.CASE_INSENSITIVE;
        }
        this.pattern = Pattern.compile(pattern, flags);
    
public RegexFileFilter(String pattern, int flags)
Construct a new regular expression filter with the specified flags.

param
pattern regular string expression to match
param
flags pattern flags - e.g. {@link Pattern#CASE_INSENSITIVE}
throws
IllegalArgumentException if the pattern is null

        if (pattern == null) {
            throw new IllegalArgumentException("Pattern is missing");
        }
        this.pattern = Pattern.compile(pattern, flags);
    
public RegexFileFilter(Pattern pattern)
Construct a new regular expression filter for a compiled regular expression

param
pattern regular expression to match
throws
IllegalArgumentException if the pattern is null

        if (pattern == null) {
            throw new IllegalArgumentException("Pattern is missing");
        }

        this.pattern = pattern;
    
Methods Summary
public booleanaccept(java.io.File dir, java.lang.String name)
Checks to see if the filename matches one of the regular expressions.

param
dir the file directory
param
name the filename
return
true if the filename matches one of the regular expressions

        return (pattern.matcher(name).matches());