Skip to content

Commit 32d0b63

Browse files
committed
expose match spans via new method
1 parent f311b15 commit 32d0b63

File tree

2 files changed

+84
-27
lines changed

2 files changed

+84
-27
lines changed

src/main/java/ru/lanwen/verbalregex/VerbalExpression.java

+22
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.Map;
99
import java.util.regex.Matcher;
1010
import java.util.regex.Pattern;
11+
import java.util.regex.MatchResult;
1112

1213
public class VerbalExpression {
1314

@@ -738,6 +739,27 @@ public List<String> getTextGroups(final String toTest, final int group) {
738739
return groups;
739740
}
740741

742+
/**
743+
* Expose all matches' spans and group spans
744+
*
745+
* See test code for an example.
746+
*
747+
* Note that each MatchResult contains all results of a single regex group,
748+
* whereas the number of MatchResult objects in the result list is equal to
749+
* the number of regex groups defined in the regex pattern.
750+
*
751+
* @param toTest - string to extract from
752+
* @return list of MatchResult objects
753+
*/
754+
public List<MatchResult> getAllGroupSpans(final String toTest) {
755+
List<MatchResult> results = new ArrayList<MatchResult>();
756+
Matcher m = pattern.matcher(toTest);
757+
while (m.find()) {
758+
results.add(m.toMatchResult());
759+
}
760+
return results;
761+
}
762+
741763
@Override
742764
public String toString() {
743765
return pattern.pattern();

src/test/java/ru/lanwen/verbalregex/BasicFunctionalityUnitTest.java

+62-27
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import java.util.List;
66

7+
import java.util.regex.MatchResult;
8+
79
import static org.hamcrest.CoreMatchers.*;
810
import static org.junit.Assert.*;
911
import static ru.lanwen.verbalregex.VerbalExpression.regex;
@@ -227,13 +229,13 @@ public void testWord() {
227229
.startOfLine()
228230
.word()
229231
.build();
230-
232+
231233
assertThat("word", testRegex, matchesTo("abc123"));
232234
assertThat("non-word", testRegex, not(matchesTo("@#")));
233235
}
234-
236+
235237
@Test
236-
public void testMultipleNoRange() {
238+
public void testMultipleNoRange() {
237239
VerbalExpression testRegexStringOnly = new VerbalExpression.Builder()
238240
.startOfLine()
239241
.multiple("abc")
@@ -247,54 +249,54 @@ public void testMultipleNoRange() {
247249
.multiple("abc", 2, 4, 8)
248250
.build();
249251
VerbalExpression[] testRegexesSameBehavior = {
250-
testRegexStringOnly,
252+
testRegexStringOnly,
251253
testRegexStringAndNull,
252254
testRegexMoreThan2Ints
253255
};
254256
for (VerbalExpression testRegex : testRegexesSameBehavior) {
255-
assertThat("abc once", testRegex,
257+
assertThat("abc once", testRegex,
256258
matchesTo("abc"));
257-
assertThat("abc more than once", testRegex,
259+
assertThat("abc more than once", testRegex,
258260
matchesTo("abcabcabc"));
259-
assertThat("no abc", testRegex,
261+
assertThat("no abc", testRegex,
260262
not(matchesTo("xyz")));
261263
}
262264
}
263-
265+
264266
@Test
265-
public void testMultipleFrom() {
267+
public void testMultipleFrom() {
266268
VerbalExpression testRegexFrom = new VerbalExpression.Builder()
267269
.startOfLine()
268270
.multiple("abc", 2)
269271
.build();
270-
assertThat("no abc", testRegexFrom,
272+
assertThat("no abc", testRegexFrom,
271273
not(matchesTo("xyz")));
272-
assertThat("abc less than 2 times", testRegexFrom,
274+
assertThat("abc less than 2 times", testRegexFrom,
273275
not(matchesTo("abc")));
274-
assertThat("abc exactly 2 times", testRegexFrom,
276+
assertThat("abc exactly 2 times", testRegexFrom,
275277
matchesTo("abcabc"));
276-
assertThat("abc more than 2 times", testRegexFrom,
278+
assertThat("abc more than 2 times", testRegexFrom,
277279
matchesTo("abcabcabc"));
278280
}
279-
281+
280282
@Test
281-
public void testMultipleFromTo() {
283+
public void testMultipleFromTo() {
282284
VerbalExpression testRegexFromTo = new VerbalExpression.Builder()
283285
.startOfLine()
284286
.multiple("abc", 2, 4)
285287
.build();
286288
assertThat("no abc", testRegexFromTo, not(matchesTo("xyz")));
287-
assertThat("abc less than 2 times", testRegexFromTo,
289+
assertThat("abc less than 2 times", testRegexFromTo,
288290
not(matchesTo("abc")));
289291
assertThat("abc exactly 2 times", testRegexFromTo, matchesTo("abcabc"));
290-
assertThat("abc between 2 and 4 times", testRegexFromTo,
292+
assertThat("abc between 2 and 4 times", testRegexFromTo,
291293
matchesTo("abcabcabc"));
292-
assertThat("abc exactly 4 times", testRegexFromTo,
294+
assertThat("abc exactly 4 times", testRegexFromTo,
293295
matchesTo("abcabcabcabc"));
294-
assertThat("abc more than 4 times", testRegexFromTo,
296+
assertThat("abc more than 4 times", testRegexFromTo,
295297
not(matchesExactly("abcabcabcabcabc")));
296298
}
297-
299+
298300
@Test
299301
public void testWithAnyCase() {
300302
VerbalExpression testRegex = new VerbalExpression.Builder()
@@ -563,7 +565,7 @@ public void zeroOreMoreSameAsAtLeast0() throws Exception {
563565
assertThat(regexWithOneOrMore, matchesTo(empty));
564566
assertThat(regexWithOneOrMore, matchesExactly(empty));
565567
}
566-
568+
567569
@Test
568570
public void testOneOf() {
569571
VerbalExpression testRegex = new VerbalExpression.Builder()
@@ -575,7 +577,7 @@ public void testOneOf() {
575577
assertThat("Starts with abc or def", testRegex, matchesTo("abczzz"));
576578
assertThat("Doesn't start with abc nor def", testRegex, not(matchesTo("xyzabc")));
577579
}
578-
580+
579581
@Test
580582
public void testOneOfWithCapture() {
581583
VerbalExpression testRegex = regex()
@@ -604,7 +606,7 @@ public void testOneOfWithClosedCapture() {
604606
assertThat(testRegex.getText("xxxabcdefzzz", 1), equalTo("abcdef"));
605607
assertThat(testRegex.getText("xxxdefzzz", 1), equalTo("def"));
606608
}
607-
609+
608610
@Test
609611
public void shouldAddMaybeWithOneOfFromAnotherBuilder() {
610612
VerbalExpression.Builder namePrefix = regex().oneOf("Mr.", "Ms.");
@@ -615,12 +617,12 @@ public void shouldAddMaybeWithOneOfFromAnotherBuilder() {
615617
.word()
616618
.oneOrMore()
617619
.build();
618-
620+
619621
assertThat("Is a name with prefix", name, matchesTo("Mr. Bond"));
620622
assertThat("Is a name without prefix", name, matchesTo("James"));
621-
623+
622624
}
623-
625+
624626
@Test
625627
public void testListOfTextGroups() {
626628
String text = "SampleHelloWorldString";
@@ -630,7 +632,7 @@ public void testListOfTextGroups() {
630632
.endCapt()
631633
.maybe("String")
632634
.build();
633-
635+
634636
List<String> groups0 = regex.getTextGroups(text, 0);
635637

636638
assertThat(groups0.get(0), equalTo("Hello"));
@@ -641,4 +643,37 @@ public void testListOfTextGroups() {
641643
assertThat(groups1.get(0), equalTo("Hello"));
642644
assertThat(groups1.get(1), equalTo("World"));
643645
}
646+
647+
@Test
648+
public void testListOfGroupSpans() {
649+
String text = "SampleHelloWorldStringHello";
650+
VerbalExpression regex = regex()
651+
.capt()
652+
.oneOf("Hello", "World")
653+
.endCapt()
654+
.maybe("String")
655+
.build();
656+
657+
List<MatchResult> results = regex.getAllGroupSpans(text);
658+
659+
assertThat(results.size(), equalTo(3));
660+
661+
assertThat(results.get(0).groupCount(), equalTo(1));
662+
assertThat(results.get(1).groupCount(), equalTo(1));
663+
assertThat(results.get(2).groupCount(), equalTo(1));
664+
665+
// Hello
666+
assertThat(results.get(0).start(1), equalTo(6));
667+
assertThat(results.get(0).end(1), equalTo(11));
668+
669+
// World
670+
assertThat(results.get(1).start(1), equalTo(11));
671+
assertThat(results.get(1).end(1), equalTo(16));
672+
673+
// Hello
674+
assertThat(results.get(2).start(1), equalTo(22));
675+
assertThat(results.get(2).end(1), equalTo(27));
676+
677+
}
678+
644679
}

0 commit comments

Comments
 (0)