com.mindprod.filter
Class RegexFilter

java.lang.Object
  extended by com.mindprod.filter.RegexFilter
All Implemented Interfaces:
java.io.FilenameFilter

public final class RegexFilter
extends java.lang.Object
implements java.io.FilenameFilter

filter out all but files (or directories) that match a given Regex Pattern.

If you are clever, can do multiple positive and negative wildcards.

See http://mindprod/quoter.html for help in creating these Strings.

The \\ means treat . Literally, not as the "any" character. You need two, because \ has special meaning in Java string literals. On the command line you need only one \.

e.g. jgloss.* is "jgloss\\.[.]*" on the command line "jgloss\.[.]*"

e.g. ?gloss.html is ".gloss\\.html" on the command line is ".gloss\.html"

e.g. to find a list of files "cat\\.txt|dog\\.txt|rabbit\\.txt" On the command line you would type "cat\.txt|dog\.txt|rabbit\.txt"

see http://mindprod.com/jgloss/regex.html

Since:
2003-05-24
Version:
2.7 2010-11-22 - add optional invert parameter on a number of the filters.
Author:
Roedy Green, Canadian Mind Products

Field Summary
static int BOTH
          code for requesting both files and directories.
static boolean DEBUGGING
          true if debugging.
static int DIRS
          code for requesting directories only
static int FILES
          Code for requesting files only.
 
Constructor Summary
RegexFilter(java.lang.String positivePattern, java.lang.String negativePattern, int want)
          constructor
 
Method Summary
 boolean accept(java.io.File dir, java.lang.String name)
          Select only files that match the Regex Pattern.
static void main(java.lang.String[] args)
          Test Harness
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEBUGGING

public static final boolean DEBUGGING
true if debugging. Enables debugging harness.

See Also:
Constant Field Values

BOTH

public static final int BOTH
code for requesting both files and directories.

See Also:
Constant Field Values

DIRS

public static final int DIRS
code for requesting directories only

See Also:
Constant Field Values

FILES

public static final int FILES
Code for requesting files only.

See Also:
Constant Field Values
Constructor Detail

RegexFilter

public RegexFilter(java.lang.String positivePattern,
                   java.lang.String negativePattern,
                   int want)
constructor

Parameters:
positivePattern - Regex pattern applied to simple filename describing files you want. Null for all.
negativePattern - Regex pattern applied to simple filename\ describing files you don't want. Null for none.
want - What do you want returned FILES, DIRS or BOTH?
Method Detail

accept

public boolean accept(java.io.File dir,
                      java.lang.String name)
Select only files that match the Regex Pattern.

Specified by:
accept in interface java.io.FilenameFilter
Parameters:
dir - the directory in which the file was found.
name - the simple name of the file
Returns:
true if and only if the name should be included in the file list; false otherwise.

main

public static void main(java.lang.String[] args)
Test Harness

Parameters:
args - parm is a TEST regex pattern positive and negative. It will display all files in the current directory that match. Note that on the command line \\ should not be doubled in regex patterns. On windows, enclose parms with blanks in "s. This won't work on all platforms.