2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
17
package org.apache.commons.io.filefilter;
20
import java.io.Serializable;
21
import java.util.regex.Pattern;
23
import org.apache.commons.io.IOCase;
26
* Filters files using supplied regular expression(s).
28
* See java.util.regex.Pattern for regex matching rules
34
* File dir = new File(".");
35
* FileFilter fileFilter = new RegexFileFilter("^.*[tT]est(-\\d+)?\\.java$");
36
* File[] files = dir.listFiles(fileFilter);
37
* for (int i = 0; i < files.length; i++) {
38
* System.out.println(files[i]);
42
* @author Oliver Siegmar
43
* @version $Revision: 606381 $
44
* @since Commons IO 1.4
46
public class RegexFileFilter extends AbstractFileFilter implements Serializable {
48
/** The regular expression pattern that will be used to match filenames */
49
private final Pattern pattern;
52
* Construct a new regular expression filter.
54
* @param pattern regular string expression to match
55
* @throws IllegalArgumentException if the pattern is null
57
public RegexFileFilter(String pattern) {
58
if (pattern == null) {
59
throw new IllegalArgumentException("Pattern is missing");
62
this.pattern = Pattern.compile(pattern);
66
* Construct a new regular expression filter with the specified flags case sensitivity.
68
* @param pattern regular string expression to match
69
* @param caseSensitivity how to handle case sensitivity, null means case-sensitive
70
* @throws IllegalArgumentException if the pattern is null
72
public RegexFileFilter(String pattern, IOCase caseSensitivity) {
73
if (pattern == null) {
74
throw new IllegalArgumentException("Pattern is missing");
77
if (caseSensitivity != null && !caseSensitivity.isCaseSensitive()) {
78
flags = Pattern.CASE_INSENSITIVE;
80
this.pattern = Pattern.compile(pattern, flags);
84
* Construct a new regular expression filter with the specified flags.
86
* @param pattern regular string expression to match
87
* @param flags pattern flags - e.g. {@link Pattern#CASE_INSENSITIVE}
88
* @throws IllegalArgumentException if the pattern is null
90
public RegexFileFilter(String pattern, int flags) {
91
if (pattern == null) {
92
throw new IllegalArgumentException("Pattern is missing");
94
this.pattern = Pattern.compile(pattern, flags);
98
* Construct a new regular expression filter for a compiled regular expression
100
* @param pattern regular expression to match
101
* @throws IllegalArgumentException if the pattern is null
103
public RegexFileFilter(Pattern pattern) {
104
if (pattern == null) {
105
throw new IllegalArgumentException("Pattern is missing");
108
this.pattern = pattern;
112
* Checks to see if the filename matches one of the regular expressions.
114
* @param dir the file directory
115
* @param name the filename
116
* @return true if the filename matches one of the regular expressions
118
public boolean accept(File dir, String name) {
119
return (pattern.matcher(name).matches());