Skip to content

Commit 545b303

Browse files
committed
Create VerbalExpressions.java
1 parent def7c59 commit 545b303

File tree

1 file changed

+239
-0
lines changed

1 file changed

+239
-0
lines changed

VerbalExpressions.java

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
import java.util.regex.Pattern;
2+
3+
class VerbalExpressions {
4+
private String prefixes, source, suffixes, pattern = "";
5+
int modifiers = Pattern.MULTILINE;
6+
Pattern p;
7+
8+
public VerbalExpressions() {
9+
10+
}
11+
12+
private String sanitize(String value) {
13+
if(value != null) return value;
14+
return Pattern.quote(value);
15+
}
16+
17+
public VerbalExpressions add(String value) {
18+
this.source = this.source != null ? this.source + value : value;
19+
if (this.source != null) {
20+
this.p = Pattern.compile(this.prefixes + this.source + this.suffixes, this.modifiers);
21+
this.pattern = this.p.pattern();
22+
}
23+
return this;
24+
}
25+
26+
public VerbalExpressions startOfLine(boolean enable) {
27+
this.prefixes = enable ? "^" : "";
28+
this.add( "" );
29+
return this;
30+
}
31+
32+
public VerbalExpressions startOfLine() {
33+
return startOfLine(true);
34+
}
35+
36+
public VerbalExpressions endOfLine(boolean enable) {
37+
this.suffixes = enable ? "$" : "";
38+
this.add( "" );
39+
return this;
40+
}
41+
42+
public VerbalExpressions endOfLine() {
43+
return endOfLine(true);
44+
}
45+
46+
public VerbalExpressions then(String value) {
47+
value = sanitize(value);
48+
this.add( "(" + value + ")" );
49+
return this;
50+
}
51+
52+
public VerbalExpressions find(String value) {
53+
this.then(value);
54+
return this;
55+
}
56+
57+
public VerbalExpressions maybe(String value) {
58+
value = sanitize(value);
59+
this.add( "(" + value + ")?" );
60+
return this;
61+
}
62+
63+
public VerbalExpressions anything() {
64+
this.add( "(.*)" );
65+
return this;
66+
}
67+
68+
public VerbalExpressions anythingBut(String value) {
69+
value = sanitize(value);
70+
this.add( "([^" + value + "]*)" );
71+
return this;
72+
}
73+
74+
public VerbalExpressions replace(String source, String value) {
75+
this.add( "" );
76+
this.source.replaceAll(pattern,value);
77+
return this;
78+
}
79+
80+
public VerbalExpressions lineBreak() {
81+
this.add( "(\\n|(\\r\\n))" );
82+
return this;
83+
}
84+
85+
public VerbalExpressions br() {
86+
this.lineBreak();
87+
return this;
88+
}
89+
90+
public VerbalExpressions tab() {
91+
this.add( "\\t" );
92+
return this;
93+
}
94+
95+
public VerbalExpressions word() {
96+
this.add( "\\w+" );
97+
return this;
98+
}
99+
100+
public VerbalExpressions anyOf(String value) {
101+
value = sanitize(value);
102+
this.add( "[" + value + "]" );
103+
return this;
104+
}
105+
106+
public VerbalExpressions any(String value) {
107+
this.anyOf(value);
108+
return this;
109+
}
110+
111+
public VerbalExpressions range(Object[] args) {
112+
String value = "[";
113+
for(int _from = 0; _from < args.length; _from += 2) {
114+
int _to = _from+1;
115+
if (args.length <= _to) break;
116+
int from = Integer.getInteger(sanitize((String)args[_from]));
117+
int to = Integer.getInteger(sanitize((String)args[_to]));
118+
119+
value += from + "-" + to;
120+
}
121+
122+
value += "]";
123+
124+
this.add(value);
125+
return this;
126+
}
127+
128+
public VerbalExpressions addModifier(char modifier) {
129+
switch (modifier) {
130+
case 'd':
131+
modifiers |= Pattern.UNIX_LINES;
132+
break;
133+
case 'i':
134+
modifiers |= Pattern.CASE_INSENSITIVE;
135+
break;
136+
case 'x':
137+
modifiers |= Pattern.COMMENTS;
138+
break;
139+
case 'm':
140+
modifiers |= Pattern.MULTILINE;
141+
break;
142+
case 's':
143+
modifiers |= Pattern.DOTALL;
144+
break;
145+
case 'u':
146+
modifiers |= Pattern.UNICODE_CASE;
147+
break;
148+
case 'U':
149+
modifiers |= Pattern.UNICODE_CHARACTER_CLASS;
150+
break;
151+
default:
152+
break;
153+
}
154+
155+
this.add( "" );
156+
return this;
157+
}
158+
159+
public VerbalExpressions removeModifier(char modifier) {
160+
switch (modifier) {
161+
case 'd':
162+
modifiers ^= Pattern.UNIX_LINES;
163+
break;
164+
case 'i':
165+
modifiers ^= Pattern.CASE_INSENSITIVE;
166+
break;
167+
case 'x':
168+
modifiers ^= Pattern.COMMENTS;
169+
break;
170+
case 'm':
171+
modifiers ^= Pattern.MULTILINE;
172+
break;
173+
case 's':
174+
modifiers ^= Pattern.DOTALL;
175+
break;
176+
case 'u':
177+
modifiers ^= Pattern.UNICODE_CASE;
178+
break;
179+
case 'U':
180+
modifiers ^= Pattern.UNICODE_CHARACTER_CLASS;
181+
break;
182+
default:
183+
break;
184+
}
185+
186+
this.add( "" );
187+
return this;
188+
}
189+
190+
public VerbalExpressions withAnyCase(boolean enable) {
191+
if (enable) this.addModifier( 'i' );
192+
else this.removeModifier( 'i' );
193+
this.add( "" );
194+
return this;
195+
}
196+
197+
public VerbalExpressions withAnyCase() {
198+
return withAnyCase(true);
199+
}
200+
201+
public VerbalExpressions searchOneLine(boolean enable) {
202+
if (enable) this.removeModifier( 'm' );
203+
else this.addModifier( 'm' );
204+
this.add( "" );
205+
return this;
206+
}
207+
208+
public VerbalExpressions multiple(String value) {
209+
value = this.sanitize(value);
210+
switch (value.charAt(0)) {
211+
case '*':
212+
case '+':
213+
break;
214+
default:
215+
value += '+';
216+
}
217+
this.add(value);
218+
return this;
219+
}
220+
221+
public VerbalExpressions or(String value) {
222+
if (this.prefixes.indexOf("(") == -1) this.prefixes += "(";
223+
if (this.suffixes.indexOf(")") == -1) this.suffixes = ")" + this.suffixes;
224+
225+
this.add( ")|(" );
226+
if (value != null) this.then(value);
227+
return this;
228+
}
229+
230+
public boolean test(String toTest) {
231+
this.add( "" );
232+
return Pattern.matches(this.pattern, toTest);
233+
}
234+
235+
public String toString() {
236+
this.add( "" );
237+
return this.pattern.toString();
238+
}
239+
}

0 commit comments

Comments
 (0)