Following example demonstrates how to split a regular expression by using Pattern.compile() method and patternname.split() method of regex.Pattern class.
import java.util.regex.Pattern;
public class PatternSplitExample {
public static void main(String args[]) {
Pattern p = Pattern.compile(" ");
String tmp = "this is the Java example";
String[] tokens = p.split(tmp);
for (int i = 0; i < tokens.length; i++) {
System.out.println(tokens[i]);
}
}
}
Result:
The above code sample will produce the following result.