/
RegexTool.java
182 lines (169 loc) · 5.91 KB
/
RegexTool.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.velocity.tools;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
/**
* Velocity Tool offering various Regex-based APIs to make it easy to manipulate regular expressions from Velocity.
*
* @version $Id$
* @since 2.0RC2
*/
public class RegexTool
{
/**
* Result of a Regex search.
*/
public class RegexResult
{
/**
* @see #getStart()
*/
public int start;
/**
* @see #getEnd()
*/
public int end;
/**
* @see #getGroup()
*/
public String group;
/**
* @param start see {@link #getStart()}
* @param end see {@link #getEnd()}
* @param group see {@link #getGroup()}
*/
public RegexResult(int start, int end, String group)
{
this.start = start;
this.end = end;
this.group = group;
}
/**
* @return the captured group
*/
public String getGroup()
{
return this.group;
}
/**
* @return the capture group's start position
*/
public int getStart()
{
return this.start;
}
/**
* @return the capture group's end position
*/
public int getEnd()
{
return this.end;
}
}
/**
* @param content the content to parse
* @param regex the regex to look for in the passed content
* @return empty list if the passed regex doesn't match the content or several {@link RegexResult} objects
* containing the matched position and matched content for all capturing groups, the first group
* representing the whole . The first object is represents the entire pattern
*/
public List<RegexResult> find(String content, String regex)
{
List<RegexResult> result = new ArrayList<>();
Matcher matcher = Pattern.compile(regex, Pattern.MULTILINE).matcher(content);
if (matcher.find()) {
for (int i = 0; i < matcher.groupCount() + 1; i++) {
result.add(new RegexResult(matcher.start(i), matcher.end(i), matcher.group(i)));
}
}
return result;
}
/**
* @param content the content to parse
* @param regex the regular expression to look for in the passed content
* @return an empty list if the passed regular expression doesn't match the content, several {@link RegexResult}
* objects containing the matched position and matched content for all capturing groups and sub-groups
* otherwise
*/
public List<List<RegexResult>> findAll(String content, String regex)
{
List<List<RegexResult>> result = new ArrayList<>();
Matcher matcher = Pattern.compile(regex, Pattern.MULTILINE).matcher(content);
while (matcher.find()) {
List<RegexResult> match = new ArrayList<>();
for (int i = 0; i < matcher.groupCount() + 1; i++) {
match.add(new RegexResult(matcher.start(i), matcher.end(i), matcher.group(i)));
}
result.add(match);
}
return result;
}
/**
* Compiles a regular expression into a java {@code Pattern} object.
*
* @param regex the textual representation of the regular expression
* @return the {@code Pattern} object corresponding to the regular expression, or {@code null} if the expression is
* invalid
* @since 2.3M1
*/
public Pattern compile(String regex)
{
try {
return Pattern.compile(regex);
} catch (PatternSyntaxException ex) {
return null;
}
}
/**
* Returns a literal pattern <code>String</code> for the specified <code>String</code>.
* <p>
* This method produces a <code>String</code> that can be used to create a <code>Pattern</code> that would match the
* string <code>s</code> as if it were a literal pattern.
* </p>
* Metacharacters or escape sequences in the input sequence will be given no special meaning.
*
* @param s The string to be literalized
* @return A literal string replacement
* @since 2.4M2
*/
public String quote(String s)
{
return Pattern.quote(s);
}
/**
* Returns a literal replacement {@code String} for the specified {@code String}. This method produces a
* {@code String} that will work as a literal replacement {@code s} in
* {@code String#replaceAll(regularExpression, s)}. The {@code String} produced will match the sequence of
* characters in {@code s} treated as a literal sequence. Slashes ('\') and dollar signs ('$') will be given no
* special meaning.
*
* @param s the string to be literalized
* @return a literal string replacement
* @since 8.2RC1
*/
public String quoteReplacement(String s)
{
return Matcher.quoteReplacement(s);
}
}