001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.io.filefilter; 018 019import java.io.File; 020import java.io.Serializable; 021import java.util.regex.Pattern; 022 023import org.apache.commons.io.IOCase; 024 025/** 026 * Filters files using supplied regular expression(s). 027 * <p> 028 * See java.util.regex.Pattern for regex matching rules. 029 * </p> 030 * 031 * <p> 032 * e.g. 033 * <pre> 034 * File dir = new File("."); 035 * FileFilter fileFilter = new RegexFileFilter("^.*[tT]est(-\\d+)?\\.java$"); 036 * File[] files = dir.listFiles(fileFilter); 037 * for (int i = 0; i < files.length; i++) { 038 * System.out.println(files[i]); 039 * } 040 * </pre> 041 * 042 * @since 1.4 043 */ 044public class RegexFileFilter extends AbstractFileFilter implements Serializable { 045 046 private static final long serialVersionUID = 4269646126155225062L; 047 /** The regular expression pattern that will be used to match file names */ 048 private final Pattern pattern; 049 050 /** 051 * Construct a new regular expression filter. 052 * 053 * @param pattern regular string expression to match 054 * @throws IllegalArgumentException if the pattern is null 055 */ 056 public RegexFileFilter(final String pattern) { 057 if (pattern == null) { 058 throw new IllegalArgumentException("Pattern is missing"); 059 } 060 061 this.pattern = Pattern.compile(pattern); 062 } 063 064 /** 065 * Construct a new regular expression filter with the specified flags case sensitivity. 066 * 067 * @param pattern regular string expression to match 068 * @param caseSensitivity how to handle case sensitivity, null means case-sensitive 069 * @throws IllegalArgumentException if the pattern is null 070 */ 071 public RegexFileFilter(final String pattern, final IOCase caseSensitivity) { 072 if (pattern == null) { 073 throw new IllegalArgumentException("Pattern is missing"); 074 } 075 int flags = 0; 076 if (caseSensitivity != null && !caseSensitivity.isCaseSensitive()) { 077 flags = Pattern.CASE_INSENSITIVE; 078 } 079 this.pattern = Pattern.compile(pattern, flags); 080 } 081 082 /** 083 * Construct a new regular expression filter with the specified flags. 084 * 085 * @param pattern regular string expression to match 086 * @param flags pattern flags - e.g. {@link Pattern#CASE_INSENSITIVE} 087 * @throws IllegalArgumentException if the pattern is null 088 */ 089 public RegexFileFilter(final String pattern, final int flags) { 090 if (pattern == null) { 091 throw new IllegalArgumentException("Pattern is missing"); 092 } 093 this.pattern = Pattern.compile(pattern, flags); 094 } 095 096 /** 097 * Construct a new regular expression filter for a compiled regular expression 098 * 099 * @param pattern regular expression to match 100 * @throws IllegalArgumentException if the pattern is null 101 */ 102 public RegexFileFilter(final Pattern pattern) { 103 if (pattern == null) { 104 throw new IllegalArgumentException("Pattern is missing"); 105 } 106 107 this.pattern = pattern; 108 } 109 110 /** 111 * Checks to see if the file name matches one of the regular expressions. 112 * 113 * @param dir the file directory (ignored) 114 * @param name the file name 115 * @return true if the file name matches one of the regular expressions 116 */ 117 @Override 118 public boolean accept(final File dir, final String name) { 119 return pattern.matcher(name).matches(); 120 } 121 122}