String text = "Hello, World! How are you?"; String[] parts = Pattern.compile(",?\\s+").split(text); for (String part : parts) { System.out.println(part); }
String text = "The quick brown fox jumps over the lazy dog."; String[] parts = Pattern.compile("(?<=\\s)(?=\\S)").split(text); for (String part : parts) { System.out.print(part + "|"); }In this example, the split method is used to split the string "The quick brown fox jumps over the lazy dog." into an array of strings based on the regular expression pattern `(?<=\s)(?=\S)`. This pattern matches any empty string that has a whitespace character before and a non-whitespace character after it. The resulting array of strings contains the following elements: "The ", "quick ", "brown ", "fox ", "jumps ", "over ", "the ", "lazy " and "dog.". Overall, the java.util.regex Pattern split method is a powerful tool for splitting strings based on regular expression patterns. It allows for both simple and complex splitting scenarios, and is highly customizable depending on the regular expression used.