Java Regular Expression String split with

Introduction

Java String class split method can work with regular expression pattern.

String[ ] split(CharSequence str) 

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

// Use split(). /* www.  j a va2s  .  com*/
import java.util.regex.Pattern;

public class Main {
  public static void main(String args[]) {

    // Match lower case words.
    Pattern pat = Pattern.compile("[ ,.!]");

    String strs[] = pat.split("CSS HTML,HTML5 12!demo2s.com.");

    for (int i = 0; i < strs.length; i++)
      System.out.println("Next token: " + strs[i]);

  }
}



PreviousNext

Related