Using split( )

You can reduce an input sequence into its individual tokens by using the split( ) method defined by Pattern. One form of the split( ) method is shown here:

String[ ] split(CharSequence str)

It processes the input sequence passed in str, reducing it into tokens based on the delimiters specified by the pattern.

For example, the following program finds tokens that are separated by spaces, commas, periods, and exclamation points:

// Use split(). 
import java.util.regex.Pattern;

public class Main {
  public static void main(String args[]) {
    // Match lowercase words.
    Pattern pat = Pattern.compile("[ ,.!]");
    String strs[] = pat.split("This is, a test!");
    for (int i = 0; i < strs.length; i++)
      System.out.println("Next token: " + strs[i]);


Next token: This
Next token: is
Next token: 
Next token: a
Next token: test
  Java Book 
    Essential Classes  

  1. Regular Expression Processing
  2. Normal character
  3. Wildcard character
  4. Using Wildcards and Quantifiers
  5. Greedy behavior
  6. Working with Classes of Characters
  7. Using replaceAll( )
  8. Using split( )
  9. Matcher: appendReplacement(StringBuffer sb,String replacement)
  10. Matcher.appendTail(StringBuffer sb)
  11. Matcher: find()
  12. Matcher: group()
  13. Matcher: group(int group)
  14. Matcher: groupCount()
  15. Matcher: lookingAt()
  16. Matcher: matches()
  17. Matcher: replaceAll(String text)
  18. Matcher: start()