Skip to content

Commit 6ee5eed

Browse files
committed
group around anyOf
1 parent 6ffaf58 commit 6ee5eed

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,6 @@ Just include a dependency in your project. For the latest version see [Maven cen
7777
<dependency>
7878
<groupId>org.tbee.regexpbuilder</groupId>
7979
<artifactId>regexpbuilder</artifactId>
80-
<version>1.0.0</version>
80+
<version>1.1.0</version>
8181
</dependency>
8282
```

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ public RegExp carriageReturn() {
110110
public RegExp lineFeed() {
111111
return RegExp.of().lineFeed();
112112
}
113+
public RegExp doubleQuote() {
114+
return RegExp.of().doubleQuote();
115+
}
113116

114117
/**
115118
* Any digit, short for [0-9]

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,10 @@ public RegExp oneOrMore(String s) {
191191
public RegExp anyOf(RegExp... regExps) {
192192
boolean first = true;
193193
for (RegExp regExp : regExps) {
194-
regExpString += (first ? "" : "|") + regExp.toString();
194+
regExpString += (first ? "(" : "|") + regExp.toString();
195195
first = false;
196196
}
197+
regExpString += ")";
197198
return this;
198199
}
199200
/**
@@ -210,7 +211,7 @@ public RegExp anyOf(String... ss) {
210211
}
211212
/**
212213
* Match any of the blocks of characters
213-
* @param objects must be a RegExp and otherwise it will be converted to a string
214+
* @param objects must be a RegExp, and otherwise it will be converted to a string
214215
* @return
215216
*/
216217
public RegExp anyOf(Object... objects) {
@@ -310,6 +311,10 @@ public RegExp lineFeed() {
310311
regExpString += "\\n";
311312
return this;
312313
}
314+
public RegExp doubleQuote() {
315+
regExpString += "\\";
316+
return this;
317+
}
313318

314319
/**
315320
* Any digit, short for [0-9]

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,30 @@ public void oneOrMoreTest() {
118118
public void anyOfTest() {
119119
RegExp regExp = RegExp.of()
120120
.anyOf("^aaa", "$bbb", "(ccc", digit().word());
121-
Assertions.assertEquals("\\^aaa|\\$bbb|\\(ccc|\\d\\w", regExp.toString());
121+
Assertions.assertEquals("(\\^aaa|\\$bbb|\\(ccc|\\d\\w)", regExp.toString());
122+
}
123+
124+
@Test
125+
public void anyOfTest2() {
126+
RegExp regExp = RegExp.of()
127+
.text("For sale: ")
128+
.anyOf("cat", "dog", "mouse", "snake", "bird")
129+
.text(" food");
130+
Assertions.assertEquals("For sale: (cat|dog|mouse|snake|bird) food", regExp.toString());
131+
Matcher matcher = regExp.toMatcher("For sale: snake food");
132+
Assertions.assertTrue(matcher.matches());
133+
}
134+
135+
@Test
136+
public void anyOfTest3() {
137+
RegExp regExp = RegExp.of()
138+
.text("For sale: ")
139+
.group("animal", anyOf("cat", "dog", "mouse", "snake", "bird"))
140+
.text(" food");
141+
Assertions.assertEquals("For sale: ((cat|dog|mouse|snake|bird)) food", regExp.toString()); // TBEERNOT; can we reduce this to one group?
142+
Matcher matcher = regExp.toMatcher("For sale: snake food");
143+
Assertions.assertTrue(matcher.matches());
144+
Assertions.assertEquals("snake", matcher.group(regExp.indexOf("animal")));
122145
}
123146

124147
@Test
@@ -228,6 +251,7 @@ public void apacheLogTest() {
228251
Assertions.assertEquals("200", matcher.group(regExp.indexOf("status")));
229252
Assertions.assertEquals("2048", matcher.group(regExp.indexOf("size")));
230253
}
254+
231255
private int countMatches(Matcher matcher) {
232256
int matches = 0;
233257
while (matcher.find()) {

0 commit comments

Comments
 (0)