All computer source code presented on this page, unless it includes attribution to another author, is provided by Ed Halley under the Artistic License. Use such code freely and without any expectation of support. I would like to know if you make anything cool with the code, or need questions answered.
python/
    bindings.py
    boards.py
    buzz.py
    caches.py
    cards.py
    constraints.py
    csql.py
    english.py
    getch.py
    getopts.py
    gizmos.py
    goals.py
    improv.py
    interpolations.py
    namespaces.py
    nihongo.py
    nodes.py
    octalplus.py
    patterns.py
    physics.py
    pids.py
    pieces.py
    quizzes.py
    recipes.py
    relays.py
    romaji.py
    ropen.py
    sheets.py
    stores.py
    strokes.py
    subscriptions.py
    svgbuild.py
    testing.py
    things.py
    timing.py
    ucsv.py
    useful.py
    uuid.py
    vectors.py
    weighted.py
java/
    CSVReader.java
    CSVWriter.java
    GlobFilenameFilter.java
    RegexFilenameFilter.java
    StringBufferOutputStream.java
    ThreadSet.java
    Throttle.java
    TracingThread.java
    Utf8ConsoleTest.java
    droid/
        ArrangeViewsTouchListener.java
        DownloadFileTask.java
perl/
    CVQM.pm
    Kana.pm
    Typo.pm
cxx/
    CCache.h
    equalish.cpp
Download RegexFilenameFilter.java
package cc.halley.java.util;

import java.io.*;
import java.util.regex.Pattern;

/**
 * A filter for filenames, given a regular expression for matching the names.
 * 
 * <p>Typical use is with the <tt>File.list()</tt> method.  Example:
 * 
 * <pre>
 *    // Find all Unix-style user configuration files in home directory.
 *    // These start with a period and end in the lowercase letters "rc".
 *    //
 *    File path = new File(System.getenv("HOME"));
 *    String names = path.list(new RegexFilenameFilter("^\\..*rc"));
 *    for (int i=0; i < names.length; i++)
 *        System.out.println(names[i]);
 * </pre>
 * 
 * <p>As with any FilenameFilter, this only matches on the basename portion
 * of a pathname.  Expressions trying to match <tt>/</tt> or <tt>\</tt>
 * in the name will fail.
 */
public class RegexFilenameFilter
        implements FilenameFilter
{
    /** The pattern. */
    private final Pattern[] patterns;

    /** Create a RegexFileFilter.
     * @param pattern regular expression pattern to match
     */
    public RegexFilenameFilter(String pattern)
    {
        this(compilePatterns(new String[] { pattern }));
    }

    /** Create a RegexFileFilter.
     * <p>Note: The patterns is verified using Matcher.matches(),
     * which means the whole filename (but excluding path) is matched.
     * @param patterns String of Pattern to match against
     */
    public RegexFilenameFilter(String[] patterns)
    {
        this(compilePatterns(patterns));
    }

    private static Pattern[] compilePatterns(String[] regexes)
    {
        Pattern[] patterns = new Pattern[regexes.length];
        for (int i = 0; i < regexes.length; i++)
        {
            patterns[i] = Pattern.compile(regexes[i]);
        }
        return patterns;
    }

    /** Create a RegexFileFilter.
     * <p>Note: The pattern is verified using Matcher.find(),
     * which means the pattern may appear anywhere within the filename.
     * To match the full filename, explicitly wrap your pattern with the
     * appropriate regular expression anchors, e.g., <tt>^pattern$</tt>.
     * @param patterns Patterns to match against
     */
    public RegexFilenameFilter(Pattern[] patterns)
    {
        this.patterns = (Pattern[])patterns.clone();
    }

    /** {@inheritDoc} */
    public boolean accept(File file, String basename)
    {
        for (int i = 0; i < patterns.length; i++)
        {
            if (patterns[i].matcher(basename).find())
                return true;
        }
        return false;
    }
    
    /**
     * Convenience function directly searches for filenames matching a regex.
     * @param path the relative or absolute path to search
     * @param regex the regular expression to be matched against filenames
     * @return an array of filenames found which match, or null for errors
     */
    public static String[] find(String path, String regex)
    {
        File dir = new File(path);
                FilenameFilter filter = new RegexFilenameFilter(regex);
                return dir.list(filter);
    }
    
    /**
     * Convenience function directly searches for filenames matching a regex.
     * <p>The directory scanned is the current working path, e.g., <tt>.</tt>
     * @param regex the regular expression to be matched against filenames
     * @return an array of filenames found which match, or null for errors
     */
    public static String[] find(String regex)
        { return find(".", regex); }
}


Contact Ed Halley by email at ed@halley.cc.
Text, code, layout and artwork are Copyright © 1996-2013 Ed Halley.
Copying in whole or in part, with author attribution, is expressly allowed.
Any references to trademarks are illustrative and are controlled by their respective owners.
Make donations with PayPal - it's fast, free and secure!