Package org.apache.commons.io.filefilter
Class RegexFileFilter
java.lang.Object
org.apache.commons.io.filefilter.AbstractFileFilter
org.apache.commons.io.filefilter.RegexFileFilter
- All Implemented Interfaces:
FileFilter,FilenameFilter,Serializable,IOFileFilter
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]);
}
- Since:
- 1.4
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final PatternThe regular expression pattern that will be used to match file namesprivate static final longFields inherited from interface org.apache.commons.io.filefilter.IOFileFilter
EMPTY_STRING_ARRAY -
Constructor Summary
ConstructorsConstructorDescriptionRegexFileFilter(String pattern) Construct a new regular expression filter.RegexFileFilter(String pattern, int flags) Construct a new regular expression filter with the specified flags.RegexFileFilter(String pattern, IOCase caseSensitivity) Construct a new regular expression filter with the specified flags case sensitivity.RegexFileFilter(Pattern pattern) Construct a new regular expression filter for a compiled regular expression -
Method Summary
Methods inherited from class org.apache.commons.io.filefilter.AbstractFileFilter
accept, toString
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
pattern
The regular expression pattern that will be used to match file names
-
-
Constructor Details
-
RegexFileFilter
Construct a new regular expression filter.- Parameters:
pattern- regular string expression to match- Throws:
IllegalArgumentException- if the pattern is null
-
RegexFileFilter
Construct a new regular expression filter with the specified flags case sensitivity.- Parameters:
pattern- regular string expression to matchcaseSensitivity- how to handle case sensitivity, null means case-sensitive- Throws:
IllegalArgumentException- if the pattern is null
-
RegexFileFilter
Construct a new regular expression filter with the specified flags.- Parameters:
pattern- regular string expression to matchflags- pattern flags - e.g.Pattern.CASE_INSENSITIVE- Throws:
IllegalArgumentException- if the pattern is null
-
RegexFileFilter
Construct a new regular expression filter for a compiled regular expression- Parameters:
pattern- regular expression to match- Throws:
IllegalArgumentException- if the pattern is null
-
-
Method Details
-
accept
Checks to see if the file name matches one of the regular expressions.- Specified by:
acceptin interfaceFilenameFilter- Specified by:
acceptin interfaceIOFileFilter- Overrides:
acceptin classAbstractFileFilter- Parameters:
dir- the file directory (ignored)name- the file name- Returns:
- true if the file name matches one of the regular expressions
-