1 package org.daisy.pipeline.nlp.impl.matchrules;
2
3 import java.util.regex.Matcher;
4 import java.util.regex.Pattern;
5
6 import org.daisy.pipeline.nlp.TextCategorizer.Category;
7 import org.daisy.pipeline.nlp.TextCategorizer.MatchMode;
8 import org.daisy.pipeline.nlp.impl.MatchRule;
9
10
11
12
13
14 public class RegexMatchRule extends MatchRule {
15
16
17
18
19
20 private Pattern mPattern;
21
22 public RegexMatchRule(Category category, int priority, boolean caseSensitive,
23 MatchMode matchMode) {
24 super(category, priority, caseSensitive, matchMode);
25 }
26
27 public void init(String regexp) {
28 switch (mMatchMode) {
29 case FULL_MATCH:
30 regexp += "$";
31 break;
32 default:
33 break;
34 }
35
36 int flags = Pattern.UNICODE_CASE | Pattern.MULTILINE | Pattern.DOTALL;
37
38
39 if (!mCaseSensitive) {
40 flags |= Pattern.CASE_INSENSITIVE;
41 }
42
43 mPattern = Pattern.compile(regexp, flags);
44 }
45
46 @Override
47 protected String match(String input) {
48 Matcher m = mPattern.matcher(input);
49
50 if (m.lookingAt()) {
51 return m.group(0);
52 }
53 return null;
54 }
55
56 @Override
57 public boolean threadsafe() {
58 return true;
59 }
60 }