Skip to content

Commit b8e6732

Browse files
committed
added range and anyOf
1 parent 02c126d commit b8e6732

File tree

5 files changed

+176
-24
lines changed

5 files changed

+176
-24
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ It becomes interesting when things are more complex, for example for a log line:
2525

2626
```java
2727
// RegExp for this: 127.0.0.1 - - [21/Jul/2014:9:55:27 -0800] "GET /home.html HTTP/1.1" 200 2048
28-
RegExpCore regExp = RegExp.of()
28+
RegExp regExp = RegExp.of()
2929
.group("ip", oneOrMore(nonWhitespace()))
3030
.text(" ")
3131
.group("client", oneOrMore(nonWhitespace()))
@@ -65,6 +65,6 @@ Just include a dependency in your project. For the latest version see [Maven cen
6565
<dependency>
6666
<groupId>org.tbee.regexpbuilder</groupId>
6767
<artifactId>regexpbuilder</artifactId>
68-
<version>1.0.0</version>
68+
<version>1.0.1</version>
6969
</dependency>
7070
```

pom.xml

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>org.tbee.regexpbuilder</groupId>
88
<artifactId>regexpbuilder</artifactId>
9-
<version>1.0.0</version>
9+
<version>1.0.1</version>
1010

1111
<properties>
1212
<maven.compiler.source>17</maven.compiler.source>
@@ -51,23 +51,11 @@
5151
</dependencyManagement>
5252

5353
<dependencies>
54-
<dependency>
55-
<groupId>org.slf4j</groupId>
56-
<artifactId>slf4j-api</artifactId>
57-
<version>2.0.6</version>
58-
</dependency>
59-
6054
<dependency>
6155
<groupId>org.junit.jupiter</groupId>
6256
<artifactId>junit-jupiter</artifactId>
6357
<scope>test</scope>
6458
</dependency>
65-
<dependency>
66-
<groupId>org.slf4j</groupId>
67-
<artifactId>slf4j-simple</artifactId>
68-
<version>2.0.6</version>
69-
<scope>test</scope>
70-
</dependency>
7159
</dependencies>
7260

7361
<build>
@@ -89,7 +77,7 @@
8977
<plugin>
9078
<groupId>org.apache.maven.plugins</groupId>
9179
<artifactId>maven-javadoc-plugin</artifactId>
92-
<version>3.4.1</version>
80+
<version>3.5.0</version>
9381
<configuration>
9482
<doclint>all,-missing</doclint>
9583
</configuration>

src/main/java/org/tbee/regexpbuilder/RE.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public RegExp group(String name, RegExp regExp) {
1616
static public RegExp text(String s) {
1717
return RegExp.of().text(s);
1818
}
19+
static public RegExp range(String fromChar, String toChar) {
20+
return RegExp.of().range(fromChar, toChar);
21+
}
1922

2023
static public RegExp oneOf(RegExp regExp) {
2124
return RegExp.of().oneOf(regExp);

src/main/java/org/tbee/regexpbuilder/RegExp.java

Lines changed: 152 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static RegExp of() {
2020

2121

2222
// -------------------------
23-
// COMPLEX
23+
// GROUP
2424

2525
private RegExp startGroup(String name) {
2626
regExpString += "(";
@@ -66,71 +66,216 @@ public int indexOf(String name) {
6666
// -------------------------
6767
// PATTERN
6868

69-
public RegExp text(String match) {
70-
regExpString += escape(match);
69+
/**
70+
* Match a specific text.
71+
* Any characters with special meaning in regular expressions will be escape.
72+
* @param s the text to match, it does not need to be (regexp) escaped.
73+
* @return
74+
*/
75+
public RegExp text(String s) {
76+
regExpString += escape(s);
77+
return this;
78+
}
79+
80+
/**
81+
* Match a range of text.
82+
* Any characters with special meaning in regular expressions will be escape.
83+
* @param fromChar the beginning character
84+
* @param toChar the beginning character
85+
* @return
86+
*/
87+
public RegExp range(String fromChar, String toChar) {
88+
if (fromChar.length() != 1 || toChar.length() != 1) {
89+
throw new IllegalArgumentException("The character parameters must be exactly 1 in length");
90+
}
91+
regExpString += "[" + escape(fromChar) + "-" + escape(toChar) + "]";
7192
return this;
7293
}
7394

95+
/**
96+
* Match one of the characters
97+
* @param regExp
98+
* @return
99+
*/
74100
public RegExp oneOf(RegExp regExp) {
75101
regExpString += "[" + regExp.toString() + "]";
76102
return this;
77103
}
78-
public RegExp oneOf(String match) {
79-
return oneOf(RegExp.of().text(match));
104+
/**
105+
* Match one of the characters
106+
* @param s
107+
* @return
108+
*/
109+
public RegExp oneOf(String s) {
110+
return oneOf(RegExp.of().text(s));
80111
}
81112

113+
/**
114+
* Match any character but the specified ones
115+
* @param regExp
116+
* @return
117+
*/
82118
public RegExp notOneOf(RegExp regExp) {
83119
regExpString += "[^" + regExp.toString() + "]";
84120
return this;
85121
}
86-
public RegExp notOneOf(String match) {
87-
return notOneOf(RegExp.of().text(match));
122+
/**
123+
* Match any character but the specified ones
124+
* @param s
125+
* @return
126+
*/
127+
public RegExp notOneOf(String s) {
128+
return notOneOf(RegExp.of().text(s));
88129
}
89130

131+
/**
132+
* These characters may be present, or not.
133+
* @param regExp
134+
* @return
135+
*/
90136
public RegExp optional(RegExp regExp) {
91137
regExpString += regExp.toString() + "?";
92138
return this;
93139
}
140+
/**
141+
* These characters may be present, or not.
142+
* @param s
143+
* @return
144+
*/
94145
public RegExp optional(String s) {
95146
return optional(RegExp.of().text(s));
96147
}
97148

149+
/**
150+
* These characters can not be present, or once, or many times
151+
* @param regExp
152+
* @return
153+
*/
98154
public RegExp zeroOrMore(RegExp regExp) {
99155
regExpString += regExp.toString() + "*";
100156
return this;
101157
}
158+
159+
/**
160+
* These characters can not be present, or once, or many times
161+
* @param s
162+
* @return
163+
*/
102164
public RegExp zeroOrMore(String s) {
103165
return zeroOrMore(RegExp.of().text(s));
104166
}
105167

168+
/**
169+
* These characters must be present once, but can be many times
170+
* @param regExp
171+
* @return
172+
*/
106173
public RegExp oneOrMore(RegExp regExp) {
107174
regExpString += regExp.toString() + "+";
108175
return this;
109176
}
177+
/**
178+
* These characters must be present once, but can be many times
179+
* @param s
180+
* @return
181+
*/
110182
public RegExp oneOrMore(String s) {
111183
return oneOrMore(RegExp.of().text(s));
112184
}
113185

186+
/**
187+
* Match any of the blocks of characters
188+
* @param regExps
189+
* @return
190+
*/
191+
public RegExp anyOf(RegExp... regExps) {
192+
boolean first = true;
193+
for (RegExp regExp : regExps) {
194+
regExpString += (first ? "" : "|") + regExp.toString();
195+
first = false;
196+
}
197+
return this;
198+
}
199+
/**
200+
* Match any of the blocks of characters
201+
* @param ss
202+
* @return
203+
*/
204+
public RegExp anyOf(String... ss) {
205+
RegExp[] regExps = new RegExp[ss.length];
206+
for (int i = 0; i < ss.length; i++) {
207+
regExps[i] = RegExp.of().text(ss[i]);
208+
}
209+
return anyOf(regExps);
210+
}
211+
/**
212+
* Match any of the blocks of characters
213+
* @param objects must be a RegExp or otherwise it will be converted to a string
214+
* @return
215+
*/
216+
public RegExp anyOf(Object... objects) {
217+
RegExp[] regExps = new RegExp[objects.length];
218+
for (int i = 0; i < objects.length; i++) {
219+
if (objects[i] instanceof RegExp regExp) {
220+
regExps[i] = regExp;
221+
}
222+
else {
223+
regExps[i] = RegExp.of().text(objects[i].toString());
224+
}
225+
}
226+
return anyOf(regExps);
227+
}
228+
229+
/**
230+
* These characters are present N times
231+
* @param regExp
232+
* @return
233+
*/
114234
public RegExp occurs(int times, RegExp regExp) {
115235
regExpString += regExp.toString() + "{" + times + "}";
116236
return this;
117237
}
238+
/**
239+
* These characters are present N times
240+
* @param s
241+
* @return
242+
*/
118243
public RegExp occurs(int times, String s) {
119244
return occurs(times, RegExp.of().text(s));
120245
}
121246

247+
/**
248+
* These characters are present at least N times
249+
* @param regExp
250+
* @return
251+
*/
122252
public RegExp occursAtLeast(int times, RegExp regExp) {
123253
regExpString += regExp.toString() + "{" + times + ",}";
124254
return this;
125255
}
256+
/**
257+
* These characters are present at least N times
258+
* @param s
259+
* @return
260+
*/
126261
public RegExp occursAtLeast(int times, String s) {
127262
return occursAtLeast(times, RegExp.of().text(s));
128263
}
129264

265+
/**
266+
* These characters are present N to M times
267+
* @param regExp
268+
* @return
269+
*/
130270
public RegExp occursBetween(int minTimes, int maxTimes, RegExp regExp) {
131271
regExpString += regExp.toString() + "{" + minTimes + "," + maxTimes + "}";
132272
return this;
133273
}
274+
/**
275+
* These characters are present N to M times
276+
* @param s
277+
* @return
278+
*/
134279
public RegExp occursBetween(int minTimes, int maxTimes, String s) {
135280
return occursBetween(minTimes, maxTimes, RegExp.of().text(s));
136281
}

src/test/java/org/tbee/regexpbuilder/RegExpTest.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ public void exactTest() {
1818
Assertions.assertEquals(2, countMatches(regExp.toMatcher("^foobar^foo")));
1919
}
2020

21+
@Test
22+
public void rangeTest() {
23+
RegExp regExp = RegExp.of()
24+
.range("0", "9");
25+
Assertions.assertEquals("[0-9]", regExp.toString());
26+
}
27+
2128
@Test
2229
public void oneOfTest() {
2330
RegExp regExp = RegExp.of()
@@ -83,6 +90,13 @@ public void oneOrMoreTest() {
8390
Assertions.assertEquals(2, countMatches(regExp.toMatcher("[foobar[foo")));
8491
}
8592

93+
@Test
94+
public void anyOfTest() {
95+
RegExp regExp = RegExp.of()
96+
.anyOf("^aaa", "$bbb", "(ccc", digit().wordChar());
97+
Assertions.assertEquals("\\^aaa|\\$bbb|\\(ccc|\\d\\w", regExp.toString());
98+
}
99+
86100
@Test
87101
public void groupTest() {
88102
RegExp regExp = RegExp.of()
@@ -152,6 +166,7 @@ public void complexTest() {
152166
@Test
153167
public void apacheLogTest() {
154168
RegExp regExp = RegExp.of()
169+
.startOfLine()
155170
.group("ip", oneOrMore(nonWhitespace()))
156171
.text(" ")
157172
.group("client", oneOrMore(nonWhitespace()))
@@ -170,7 +185,8 @@ public void apacheLogTest() {
170185
.text("\" ")
171186
.group("status", oneOrMore(digit()))
172187
.whitespace()
173-
.group("size", oneOrMore(digit()));
188+
.group("size", oneOrMore(digit()))
189+
.endOfLine();
174190

175191
// https://github.com/sgreben/regex-builder#examples
176192
System.out.println(regExp.toString());

0 commit comments

Comments
 (0)