@@ -54,22 +54,57 @@ public void test2() {
5454 public void test3 () {
5555
5656 String poem =
57- "Twas brillig, and the slithy toves/ n" +
58- "Did gyre and gimble in the wabe./ n" +
59- "All mimsy were the borogoves,/ n" +
60- "And the mome raths outgrabe./n/ n" +
61- "Beware the Jabberwock, my son,/ n" +
62- "The jaws that bite, the claws that catch./ n" +
63- "Beware the Jubjub bird, and shun/ n" +
57+ "Twas brillig, and the slithy toves\ n " +
58+ "Did gyre and gimble in the wabe.\ n " +
59+ "All mimsy were the borogoves,\ n " +
60+ "And the mome raths outgrabe.\n \ n " +
61+ "Beware the Jabberwock, my son,\ n " +
62+ "The jaws that bite, the claws that catch.\ n " +
63+ "Beware the Jubjub bird, and shun\ n " +
6464 "The frumious Bandersnatch." ;
6565
6666 Pattern pattern = Pattern .compile ("(?m)(\\ S+)\\ s+((\\ S+)\\ s+(\\ S+))" );
6767
6868 Matcher matcher = pattern .matcher (poem );
6969
7070 while (matcher .find ()) {
71- for (int i = 0 ; i <= matcher .groupCount (); i ++) {
72- System .out .println (matcher .group (i ));
71+ int count = matcher .groupCount ();
72+ for (int i = 0 ; i <= count ; i ++) {
73+ String result = matcher .group (i );
74+ System .out .println (result );
75+ }
76+ }
77+ }
78+
79+
80+ @ Test
81+ public void test4 () {
82+ String [] input = new String []{"Java has regular expressions in 1.4" , "regular expressions now expressing in Java" , "Java represses oracular expressions" };
83+ Pattern p1 = Pattern .compile ("re\\ w*" ),
84+ p2 = Pattern .compile ("Java.*" );
85+ for (int i = 0 ; i < input .length ; i ++) {
86+ System .out .println ("input " + i + ": " + input [i ]);
87+ Matcher m1 = p1 .matcher (input [i ]),
88+ m2 = p2 .matcher (input [i ]);
89+ while (m1 .find ()) {
90+ System .out .println ("m1.find() '" + m1 .group () + "' start = " + m1 .start () + " end = " + m1 .end ());
91+ }
92+ while (m2 .find ()) {
93+ System .out .println ("m2.find() '" + m2 .group () + "' start = " + m2 .start () + " end = " + m2 .end ());
94+ }
95+ // No reset() necessary
96+ if (m1 .lookingAt ()) {
97+ System .out .println ("m1.lookingAt() start = " + m1 .start () + " end = " + m1 .end ());
98+ }
99+ if (m2 .lookingAt ()) {
100+ System .out .println ("m2.lookingAt() start = " + m2 .start () + " end = " + m2 .end ());
101+ }
102+ // No reset() necessary
103+ if (m1 .matches ()) {
104+ System .out .println ("m1.matches() start = " + m1 .start () + " end = " + m1 .end ());
105+ }
106+ if (m2 .matches ()) {
107+ System .out .println ("m2.matches() start = " + m2 .start () + " end = " + m2 .end ());
73108 }
74109 }
75110 }
0 commit comments