4
4
5
5
import java .util .List ;
6
6
7
+ import java .util .regex .MatchResult ;
8
+
7
9
import static org .hamcrest .CoreMatchers .*;
8
10
import static org .junit .Assert .*;
9
11
import static ru .lanwen .verbalregex .VerbalExpression .regex ;
@@ -227,13 +229,13 @@ public void testWord() {
227
229
.startOfLine ()
228
230
.word ()
229
231
.build ();
230
-
232
+
231
233
assertThat ("word" , testRegex , matchesTo ("abc123" ));
232
234
assertThat ("non-word" , testRegex , not (matchesTo ("@#" )));
233
235
}
234
-
236
+
235
237
@ Test
236
- public void testMultipleNoRange () {
238
+ public void testMultipleNoRange () {
237
239
VerbalExpression testRegexStringOnly = new VerbalExpression .Builder ()
238
240
.startOfLine ()
239
241
.multiple ("abc" )
@@ -247,54 +249,54 @@ public void testMultipleNoRange() {
247
249
.multiple ("abc" , 2 , 4 , 8 )
248
250
.build ();
249
251
VerbalExpression [] testRegexesSameBehavior = {
250
- testRegexStringOnly ,
252
+ testRegexStringOnly ,
251
253
testRegexStringAndNull ,
252
254
testRegexMoreThan2Ints
253
255
};
254
256
for (VerbalExpression testRegex : testRegexesSameBehavior ) {
255
- assertThat ("abc once" , testRegex ,
257
+ assertThat ("abc once" , testRegex ,
256
258
matchesTo ("abc" ));
257
- assertThat ("abc more than once" , testRegex ,
259
+ assertThat ("abc more than once" , testRegex ,
258
260
matchesTo ("abcabcabc" ));
259
- assertThat ("no abc" , testRegex ,
261
+ assertThat ("no abc" , testRegex ,
260
262
not (matchesTo ("xyz" )));
261
263
}
262
264
}
263
-
265
+
264
266
@ Test
265
- public void testMultipleFrom () {
267
+ public void testMultipleFrom () {
266
268
VerbalExpression testRegexFrom = new VerbalExpression .Builder ()
267
269
.startOfLine ()
268
270
.multiple ("abc" , 2 )
269
271
.build ();
270
- assertThat ("no abc" , testRegexFrom ,
272
+ assertThat ("no abc" , testRegexFrom ,
271
273
not (matchesTo ("xyz" )));
272
- assertThat ("abc less than 2 times" , testRegexFrom ,
274
+ assertThat ("abc less than 2 times" , testRegexFrom ,
273
275
not (matchesTo ("abc" )));
274
- assertThat ("abc exactly 2 times" , testRegexFrom ,
276
+ assertThat ("abc exactly 2 times" , testRegexFrom ,
275
277
matchesTo ("abcabc" ));
276
- assertThat ("abc more than 2 times" , testRegexFrom ,
278
+ assertThat ("abc more than 2 times" , testRegexFrom ,
277
279
matchesTo ("abcabcabc" ));
278
280
}
279
-
281
+
280
282
@ Test
281
- public void testMultipleFromTo () {
283
+ public void testMultipleFromTo () {
282
284
VerbalExpression testRegexFromTo = new VerbalExpression .Builder ()
283
285
.startOfLine ()
284
286
.multiple ("abc" , 2 , 4 )
285
287
.build ();
286
288
assertThat ("no abc" , testRegexFromTo , not (matchesTo ("xyz" )));
287
- assertThat ("abc less than 2 times" , testRegexFromTo ,
289
+ assertThat ("abc less than 2 times" , testRegexFromTo ,
288
290
not (matchesTo ("abc" )));
289
291
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 ,
291
293
matchesTo ("abcabcabc" ));
292
- assertThat ("abc exactly 4 times" , testRegexFromTo ,
294
+ assertThat ("abc exactly 4 times" , testRegexFromTo ,
293
295
matchesTo ("abcabcabcabc" ));
294
- assertThat ("abc more than 4 times" , testRegexFromTo ,
296
+ assertThat ("abc more than 4 times" , testRegexFromTo ,
295
297
not (matchesExactly ("abcabcabcabcabc" )));
296
298
}
297
-
299
+
298
300
@ Test
299
301
public void testWithAnyCase () {
300
302
VerbalExpression testRegex = new VerbalExpression .Builder ()
@@ -563,7 +565,7 @@ public void zeroOreMoreSameAsAtLeast0() throws Exception {
563
565
assertThat (regexWithOneOrMore , matchesTo (empty ));
564
566
assertThat (regexWithOneOrMore , matchesExactly (empty ));
565
567
}
566
-
568
+
567
569
@ Test
568
570
public void testOneOf () {
569
571
VerbalExpression testRegex = new VerbalExpression .Builder ()
@@ -575,7 +577,7 @@ public void testOneOf() {
575
577
assertThat ("Starts with abc or def" , testRegex , matchesTo ("abczzz" ));
576
578
assertThat ("Doesn't start with abc nor def" , testRegex , not (matchesTo ("xyzabc" )));
577
579
}
578
-
580
+
579
581
@ Test
580
582
public void testOneOfWithCapture () {
581
583
VerbalExpression testRegex = regex ()
@@ -604,7 +606,7 @@ public void testOneOfWithClosedCapture() {
604
606
assertThat (testRegex .getText ("xxxabcdefzzz" , 1 ), equalTo ("abcdef" ));
605
607
assertThat (testRegex .getText ("xxxdefzzz" , 1 ), equalTo ("def" ));
606
608
}
607
-
609
+
608
610
@ Test
609
611
public void shouldAddMaybeWithOneOfFromAnotherBuilder () {
610
612
VerbalExpression .Builder namePrefix = regex ().oneOf ("Mr." , "Ms." );
@@ -615,12 +617,12 @@ public void shouldAddMaybeWithOneOfFromAnotherBuilder() {
615
617
.word ()
616
618
.oneOrMore ()
617
619
.build ();
618
-
620
+
619
621
assertThat ("Is a name with prefix" , name , matchesTo ("Mr. Bond" ));
620
622
assertThat ("Is a name without prefix" , name , matchesTo ("James" ));
621
-
623
+
622
624
}
623
-
625
+
624
626
@ Test
625
627
public void testListOfTextGroups () {
626
628
String text = "SampleHelloWorldString" ;
@@ -630,7 +632,7 @@ public void testListOfTextGroups() {
630
632
.endCapt ()
631
633
.maybe ("String" )
632
634
.build ();
633
-
635
+
634
636
List <String > groups0 = regex .getTextGroups (text , 0 );
635
637
636
638
assertThat (groups0 .get (0 ), equalTo ("Hello" ));
@@ -641,4 +643,37 @@ public void testListOfTextGroups() {
641
643
assertThat (groups1 .get (0 ), equalTo ("Hello" ));
642
644
assertThat (groups1 .get (1 ), equalTo ("World" ));
643
645
}
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
+
644
679
}
0 commit comments