1 package net.sourceforge.pmd.util.filter;
2
3 import java.util.regex.Pattern;
4
5 /**
6 * A filter to which uses a regular expression to match upon Strings.
7 */
8 public class RegexStringFilter implements Filter<String> {
9
10 protected String regex;
11
12 protected Pattern pattern;
13
14 public RegexStringFilter() {
15 }
16
17 public RegexStringFilter(String regex) {
18 this.regex = regex;
19 }
20
21 public String getRegex() {
22 return regex;
23 }
24
25 public void setRegex(String regex) {
26 this.regex = regex;
27 this.pattern = null;
28 }
29
30 public boolean filter(String obj) {
31 if (pattern == null) {
32 pattern = Pattern.compile(regex);
33 }
34 return pattern.matcher(obj).matches();
35 }
36
37 public String toString() {
38 return "matches " + regex;
39 }
40 }