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 * @version $Id: RegexFileFilter.java 1642757 2014-12-01 21:09:30Z sebb $ 043 * @since 1.4 044 */ 045public class RegexFileFilter extends AbstractFileFilter implements Serializable { 046 047 private static final long serialVersionUID = 4269646126155225062L; 048 /** The regular expression pattern that will be used to match filenames */ 049 private final Pattern pattern; 050 051 /** 052 * Construct a new regular expression filter. 053 * 054 * @param pattern regular string expression to match 055 * @throws IllegalArgumentException if the pattern is null 056 */ 057 public RegexFileFilter(final String pattern) { 058 if (pattern == null) { 059 throw new IllegalArgumentException("Pattern is missing"); 060 } 061 062 this.pattern = Pattern.compile(pattern); 063 } 064 065 /** 066 * Construct a new regular expression filter with the specified flags case sensitivity. 067 * 068 * @param pattern regular string expression to match 069 * @param caseSensitivity how to handle case sensitivity, null means case-sensitive 070 * @throws IllegalArgumentException if the pattern is null 071 */ 072 public RegexFileFilter(final String pattern, final IOCase caseSensitivity) { 073 if (pattern == null) { 074 throw new IllegalArgumentException("Pattern is missing"); 075 } 076 int flags = 0; 077 if (caseSensitivity != null && !caseSensitivity.isCaseSensitive()) { 078 flags = Pattern.CASE_INSENSITIVE; 079 } 080 this.pattern = Pattern.compile(pattern, flags); 081 } 082 083 /** 084 * Construct a new regular expression filter with the specified flags. 085 * 086 * @param pattern regular string expression to match 087 * @param flags pattern flags - e.g. {@link Pattern#CASE_INSENSITIVE} 088 * @throws IllegalArgumentException if the pattern is null 089 */ 090 public RegexFileFilter(final String pattern, final int flags) { 091 if (pattern == null) { 092 throw new IllegalArgumentException("Pattern is missing"); 093 } 094 this.pattern = Pattern.compile(pattern, flags); 095 } 096 097 /** 098 * Construct a new regular expression filter for a compiled regular expression 099 * 100 * @param pattern regular expression to match 101 * @throws IllegalArgumentException if the pattern is null 102 */ 103 public RegexFileFilter(final Pattern pattern) { 104 if (pattern == null) { 105 throw new IllegalArgumentException("Pattern is missing"); 106 } 107 108 this.pattern = pattern; 109 } 110 111 /** 112 * Checks to see if the filename matches one of the regular expressions. 113 * 114 * @param dir the file directory (ignored) 115 * @param name the filename 116 * @return true if the filename matches one of the regular expressions 117 */ 118 @Override 119 public boolean accept(final File dir, final String name) { 120 return pattern.matcher(name).matches(); 121 } 122 123}