org.apache.commons.io.filefilter

Class RegexFileFilter

  • All Implemented Interfaces:
    FileFilter, FilenameFilter, Serializable, FileVisitor< Path>, PathFilter, PathVisitor, IOFileFilter

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

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

    Using Classic IO

    e.g.

     File dir = new File(".");
     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 = Paths.get("");
     final AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(new RegexFileFilter("^.*[tT]est(-\\d+)?\\.java$"));
     //
     // Walk one dir
     Files.walkFileTree(dir, Collections.emptySet(), 1, visitor);
     System.out.println(visitor.getPathCounters());
     System.out.println(visitor.getFileList());
     //
     visitor.getPathCounters().reset();
     //
     // Walk dir tree
     Files.walkFileTree(dir, visitor);
     System.out.println(visitor.getPathCounters());
     System.out.println(visitor.getDirList());
     System.out.println(visitor.getFileList());
     
    Since:
    1.4
    See Also:
    Serialized Form
    • Constructor Detail

      • RegexFileFilter

        public RegexFileFilter(Pattern pattern)
        Constructs a new regular expression filter for a compiled regular expression
        Parameters:
        pattern - regular expression to match.
        Throws:
        IllegalArgumentException - if the pattern is null.
      • RegexFileFilter

        public RegexFileFilter(Pattern pattern,
                               Function<Path,String> pathToString)
        Constructs 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:
        IllegalArgumentException - if the pattern is null.
        Since:
        2.10.0
      • RegexFileFilter

        public RegexFileFilter(String pattern)
        Constructs a new regular expression filter.
        Parameters:
        pattern - regular string expression to match
        Throws:
        IllegalArgumentException - if the pattern is null
      • RegexFileFilter

        public RegexFileFilter(String pattern,
                               int flags)
        Constructs 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
      • RegexFileFilter

        public RegexFileFilter(String pattern,
                               IOCase caseSensitivity)
        Constructs a new regular expression filter with the specified flags case sensitivity.
        Parameters:
        pattern - regular string expression to match
        caseSensitivity - how to handle case sensitivity, null means case-sensitive
        Throws:
        IllegalArgumentException - if the pattern is null
    • Method Detail

      • accept

        public boolean accept(File dir,
                              String name)
        Checks to see if the file name matches one of the regular expressions.
        Specified by:
        accept in interface  FilenameFilter
        Specified by:
        accept in interface  IOFileFilter
        Overrides:
        accept in class  AbstractFileFilter
        Parameters:
        dir - the file directory (ignored)
        name - the file name
        Returns:
        true if the file name matches one of the regular expressions
      • accept

        public FileVisitResult accept(Path path,
                                      BasicFileAttributes attributes)
        Checks to see if the file name matches one of the regular expressions.
        Specified by:
        accept in interface  PathFilter
        Specified by:
        accept in interface  IOFileFilter
        Parameters:
        path - the path
        attributes - the path attributes
        Returns:
        true if the file name matches one of the regular expressions

Copyright © 2002–2021 The Apache Software Foundation. All rights reserved.