/bundles/plugins-trunk/RubyPlugin/src/org/jedit/ruby/utils/RegularExpression.java
Java | 124 lines | 93 code | 28 blank | 3 comment | 8 complexity | 24b8e5440e75200b20dd5b652d6b2156 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
1package org.jedit.ruby.utils;
2
3import java.util.regex.Pattern;
4import java.util.regex.MatchResult;
5import java.util.regex.Matcher;
6import java.util.List;
7import java.util.ArrayList;
8
9
10/**
11 * @author robmckinnon at users,sourceforge,net
12 */
13public class RegularExpression {
14
15 private Pattern pattern;
16
17 public RegularExpression() {
18 initPattern(getPattern());
19 }
20
21 public RegularExpression(String pattern) {
22 initPattern(pattern);
23 }
24
25 private void initPattern(String pattern) {
26 this.pattern = Pattern.compile(pattern);
27 }
28
29 public boolean hasMatch(String text) {
30 return pattern.matcher(text).find();
31 }
32
33 public boolean isMatch(String text) {
34 return pattern.matcher(text).matches();
35 }
36
37 protected String getPattern() {
38 throw new IllegalStateException("override to use");
39 }
40
41 public MatchResult firstMatch(String line) {
42 Matcher matcher = pattern.matcher(line);
43 if (matcher.find()) {
44 return new MatchResultWithoutNullGroups(matcher.toMatchResult());
45 } else {
46 return null;
47 }
48 }
49
50 public MatchResult lastMatch(String line, int startGroupIndex) {
51 Matcher matcher = pattern.matcher(line);
52 MatchResult match = null;
53 int start = 0;
54
55 while (matcher.find(start)) {
56 match = matcher.toMatchResult();
57 start = match.start(startGroupIndex);
58 }
59
60 return match == null ? null : new MatchResultWithoutNullGroups(match);
61 }
62
63 public MatchResult[] getAllMatchResults(String line) {
64 Matcher matcher = pattern.matcher(line);
65 List<MatchResult> matches = new ArrayList<MatchResult>();
66 while (matcher.find()) {
67 matches.add(matcher.toMatchResult());
68 }
69 return matches.toArray(new MatchResult[matches.size()]);
70 }
71
72 public int allMatchResults(String line) {
73 int count = 0;
74 Matcher matcher = pattern.matcher(line);
75
76 if (matcher.matches()) {
77 boolean nextMatch = matcher.find(0);
78
79 while (nextMatch) {
80 count++;
81 nextMatch = matcher.find(matcher.end());
82 }
83 }
84
85 return count;
86 }
87
88 private static class MatchResultWithoutNullGroups implements MatchResult {
89 private final MatchResult result;
90
91 public MatchResultWithoutNullGroups(MatchResult result) {
92 this.result = result;
93 }
94
95 public int start() {
96 return result.start();
97 }
98
99 public int start(int group) {
100 return result.start(group);
101 }
102
103 public int end() {
104 return result.end();
105 }
106
107 public int end(int group) {
108 return result.end(group);
109 }
110
111 public String group() {
112 return result.group();
113 }
114
115 public String group(int group) {
116 String match = result.group(group);
117 return match == null ? "" : match;
118 }
119
120 public int groupCount() {
121 return result.groupCount();
122 }
123 }
124}