1
1
import java .util .regex .Pattern ;
2
2
3
3
class VerbalExpression {
4
- private String prefixes , source , suffixes , pattern = "" ;
4
+ private String prefixes = "" , source = "" , suffixes = "" , pattern = "" ;
5
5
private int modifiers = Pattern .MULTILINE ;
6
6
7
+ public VerbalExpression () {
8
+ this .updatePattern ();
9
+ }
10
+
7
11
private String sanitize (String value ) {
8
12
if (value == null )
9
13
return value ;
10
14
return Pattern .quote (value );
11
15
}
12
16
13
17
public VerbalExpression add (String value ) {
14
- this .source = this .source != null ? this .source + value : value ;
15
- if (this .source != null ) {
16
- Pattern p = Pattern .compile (this .prefixes + this .source + this .suffixes , this .modifiers );
17
- this .pattern = p .pattern ();
18
- }
18
+ this .source += value ;
19
+ return this .updatePattern ();
20
+ }
21
+
22
+ public VerbalExpression updatePattern () {
23
+ Pattern p = Pattern .compile (this .prefixes + this .source + this .suffixes , this .modifiers );
24
+ this .pattern = p .pattern ();
19
25
return this ;
20
26
}
21
27
22
28
public VerbalExpression startOfLine (boolean enable ) {
23
29
this .prefixes = enable ? "^" : "" ;
24
- this .add ( "" );
30
+ this .updatePattern ( );
25
31
return this ;
26
32
}
27
33
@@ -31,7 +37,7 @@ public VerbalExpression startOfLine() {
31
37
32
38
public VerbalExpression endOfLine (boolean enable ) {
33
39
this .suffixes = enable ? "$" : "" ;
34
- this .add ( "" );
40
+ this .updatePattern ( );
35
41
return this ;
36
42
}
37
43
@@ -79,7 +85,7 @@ public VerbalExpression somethingBut(String value) {
79
85
}
80
86
81
87
public VerbalExpression replace (String source , String value ) {
82
- this .add ( "" );
88
+ this .updatePattern ( );
83
89
this .source .replaceAll (pattern ,value );
84
90
return this ;
85
91
}
@@ -159,7 +165,7 @@ public VerbalExpression addModifier(char modifier) {
159
165
break ;
160
166
}
161
167
162
- this .add ( "" );
168
+ this .updatePattern ( );
163
169
return this ;
164
170
}
165
171
@@ -190,14 +196,14 @@ public VerbalExpression removeModifier(char modifier) {
190
196
break ;
191
197
}
192
198
193
- this .add ( "" );
199
+ this .updatePattern ( );
194
200
return this ;
195
201
}
196
202
197
203
public VerbalExpression withAnyCase (boolean enable ) {
198
204
if (enable ) this .addModifier ( 'i' );
199
205
else this .removeModifier ( 'i' );
200
- this .add ( "" );
206
+ this .updatePattern ( );
201
207
return this ;
202
208
}
203
209
@@ -208,7 +214,7 @@ public VerbalExpression withAnyCase() {
208
214
public VerbalExpression searchOneLine (boolean enable ) {
209
215
if (enable ) this .removeModifier ( 'm' );
210
216
else this .addModifier ( 'm' );
211
- this .add ( "" );
217
+ this .updatePattern ( );
212
218
return this ;
213
219
}
214
220
@@ -235,12 +241,10 @@ public VerbalExpression or(String value) {
235
241
}
236
242
237
243
public boolean test (String toTest ) {
238
- this .add ("" );
239
244
return Pattern .matches (this .pattern , toTest );
240
245
}
241
246
242
247
public String toString () {
243
- this .add ("" );
244
248
return this .pattern .toString ();
245
249
}
246
250
}
0 commit comments