Matcher Example of Groups

From Java Example Source Code

Jump to: navigation, search

Contents

[edit] Overview - Matcher Example of Groups

This is a example of Java program.

[edit] Java Source Code

  • Package: com.bruceeckel
  • File: Groups.java
package com.bruceeckel;
 
//: c12:Groups.java
//From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
//www.BruceEckel.com. See copyright notice in CopyRight.txt.
 
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class Groups {
    static public final String poem = "Twas brillig, and the slithy toves\n" + "Did gyre and gimble in the wabe.\n" + "All mimsy were the borogoves,\n" + "And the mome raths outgrabe.\n\n" + "Beware the Jabberwock, my son,\n" + "The jaws that bite, the claws that catch.\n" + "Beware the Jubjub bird, and shun\n" + "The frumious Bandersnatch.";
 
    public static void main(String[] args) {
	Matcher m = Pattern.compile("(?m)(\\S+)\\s+((\\S+)\\s+(\\S+))$").matcher(poem);
	while (m.find()) {
	    for (int j = 0; j <= m.groupCount(); j++)
		System.out.print("[" + m.group(j) + "]");
	    System.out.println();
	}
 
    }
}

[edit] What Result You Can Get

Run the program, you will get:


[the slithy toves][the][slithy toves][slithy][toves]
[in the wabe.][in][the wabe.][the][wabe.]
[were the borogoves,][were][the borogoves,][the][borogoves,]
[mome raths outgrabe.][mome][raths outgrabe.][raths][outgrabe.]
[Jabberwock, my son,][Jabberwock,][my son,][my][son,]
[claws that catch.][claws][that catch.][that][catch.]
[bird, and shun][bird,][and shun][and][shun]
[The frumious Bandersnatch.][The][frumious Bandersnatch.][frumious][Bandersnatch.]

[edit] Required External Library for this Java Example

Need nothing.

http://www.ase2009.com/ online casino 132 http://www.hellzyea.com/health health insurance 8DDD http://www.yourautoinsurancesite.com/ auto insurance quotes 135811 http://www.makemeasammich.com/ auto insurance mcnxu

[edit] Question & Answer

Any question?

Click edit and post your question or answer here.


Personal tools