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,- FileVisitor<Path>,- PathMatcher,- PathFilter,- PathVisitor,- IOFileFilter
Filters files using supplied regular expression(s).
 
See java.util.regex.Pattern for regex matching rules.
Using Classic IO
e.g.
 File dir = FileUtils.current();
 FileFilter fileFilter = new RegexFileFilter("^.*[tT]est(-\\d+)?\\.java$");
 File[] files = dir.listFiles(fileFilter);
 for (String file : files) {
     System.out.println(file);
 }
 
 Using NIO
 final Path dir = PathUtils.current();
 final AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(new RegexFileFilter("^.*[tT]est(-\\d+)?\\.java$"));
 //
 // Walk one directory
 Files.walkFileTree(dir, Collections.emptySet(), 1, visitor);
 System.out.println(visitor.getPathCounters());
 System.out.println(visitor.getFileList());
 //
 visitor.getPathCounters().reset();
 //
 // Walk directory tree
 Files.walkFileTree(dir, visitor);
 System.out.println(visitor.getPathCounters());
 System.out.println(visitor.getDirList());
 System.out.println(visitor.getFileList());
 
 Deprecating Serialization
Serialization is deprecated and will be removed in 3.0.
- Since:
- 1.4
- See Also:
- 
Field SummaryFields inherited from interface org.apache.commons.io.filefilter.IOFileFilterEMPTY_STRING_ARRAY
- 
Constructor SummaryConstructorsConstructorDescriptionRegexFileFilter(String pattern) Constructs a new regular expression filter.RegexFileFilter(String pattern, int flags) Constructs a new regular expression filter with the specified flags.RegexFileFilter(String pattern, IOCase ioCase) Constructs a new regular expression filter with the specified flags case sensitivity.RegexFileFilter(Pattern pattern) Constructs a new regular expression filter for a compiled regular expressionRegexFileFilter(Pattern pattern, Function<Path, String> pathToString) Constructs a new regular expression filter for a compiled regular expression
- 
Method SummaryModifier and TypeMethodDescriptionbooleanTests to see if the file name matches one of the regular expressions.accept(Path path, BasicFileAttributes attributes) Tests to see if the file name matches one of the regular expressions.toString()Returns a debug string.Methods inherited from class org.apache.commons.io.filefilter.AbstractFileFilteraccept, handle, postVisitDirectory, preVisitDirectory, visitFile, visitFileFailedMethods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.apache.commons.io.filefilter.IOFileFilterand, matches, negate, or
- 
Constructor Details- 
RegexFileFilterConstructs a new regular expression filter for a compiled regular expression- Parameters:
- pattern- regular expression to match.
- Throws:
- NullPointerException- if the pattern is null.
 
- 
RegexFileFilterConstructs a new regular expression filter for a compiled regular expression- Parameters:
- pattern- regular expression to match.
- pathToString- How convert a path to a string.
- Throws:
- NullPointerException- if the pattern is null.
- Since:
- 2.10.0
 
- 
RegexFileFilterConstructs a new regular expression filter.- Parameters:
- pattern- regular string expression to match
- Throws:
- NullPointerException- if the pattern is null
 
- 
RegexFileFilterConstructs a new regular expression filter with the specified flags.- Parameters:
- pattern- regular string expression to match
- flags- pattern flags - e.g.- Pattern.CASE_INSENSITIVE
- Throws:
- IllegalArgumentException- if the pattern is null
 
- 
RegexFileFilterConstructs a new regular expression filter with the specified flags case sensitivity.- Parameters:
- pattern- regular string expression to match
- ioCase- how to handle case sensitivity, null means case-sensitive
- Throws:
- IllegalArgumentException- if the pattern is null
 
 
- 
- 
Method Details- 
acceptTests to see if the file name matches one of the regular expressions.- Specified by:
- acceptin interface- FilenameFilter
- Specified by:
- acceptin interface- IOFileFilter
- Overrides:
- acceptin class- AbstractFileFilter
- Parameters:
- dir- the file directory (ignored)
- name- the file name
- Returns:
- true if the file name matches one of the regular expressions
 
- 
acceptTests to see if the file name matches one of the regular expressions.- Specified by:
- acceptin interface- IOFileFilter
- Specified by:
- acceptin interface- PathFilter
- Parameters:
- path- the path
- attributes- the path's basic attributes (may be null).
- Returns:
- true if the file name matches one of the regular expressions
 
- 
toStringReturns a debug string.- Overrides:
- toStringin class- AbstractFileFilter
- Returns:
- a String representation
- Since:
- 2.10.0
 
 
-