String[] ops = str.split("\\s*[a-zA-Z]+\\s*");
String[] notops = str.split("\\s*[^a-zA-Z]+\\s*");
String[] res = new String[ops.length+notops.length-1];
for(int i=0; i<res.length; i++) res[i] = i%2==0 ? notops[i/2] : ops[i/2+1];

This should do it. Everything nicely stored in res.

Answer from mmaag on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-split-string-using-regex
Java - Split String Using Regex - GeeksforGeeks
July 23, 2025 - Example 1: Here, we are using the most simple split() method with the regex \\d+, which splits the string wherever one or more digits appear. ... // Java program to split a string by digits public class SplitString{ public static void main(String[] args) { String s = "apple123banana456cherry"; // Split by one or more digits String[] p = s.split("\\d+"); for (String part : p) { System.out.println(part); } } }
🌐
How to do in Java
howtodoinjava.com › home › string › java string split(): limit, regex escaping and empty strings
Java String split(): Limit, Regex Escaping and Empty Strings
October 12, 2023 - The Java String split() method breaks a string around every match of a regular expression and returns the parts as a String array, and the overload split(regex, limit) also controls how many parts we get and whether trailing empty strings stay ...
🌐
W3Schools
w3schools.com › java › ref_string_split.asp
Java String split() Method
The split() method splits a string into an array of substrings using a regular expression as the separator. If a limit is specified, the returned array will not be longer than the limit.
🌐
GeeksforGeeks
geeksforgeeks.org › java › split-string-java-examples
Java String split() Method - GeeksforGeeks
May 13, 2026 - The split() method in Java is used to divide a string into multiple parts based on a specified delimiter (regular expression). It returns an array of substrings after splitting the original string.
🌐
Tutorialspoint
tutorialspoint.com › java › lang › string_split.htm
Java - String split() Method
Following is the syntax for Java String split() method − · public String[] split(String regex) // first syntax or, public String[] split(String regex, int limit) // second syntax
🌐
TutorialsPoint
tutorialspoint.com › javaregex › javaregex_pattern_split.htm
java.util.regex.Pattern.split() Method
package com.tutorialspoint; import java.util.regex.Pattern; public class PatternDemo { private static String REGEX = ":"; private static String INPUT = "boo:and:foo"; public static void main(String[] args) { Pattern pattern = Pattern.compile(REGEX); String[] result = pattern.split(INPUT); for(String data:result){ System.out.println(data); } } }
Find elsewhere
🌐
Mkyong
mkyong.com › home › java › how to split a string in java
How to split a string in Java | mkyong.com
February 9, 2022 - The example uses the regex pattern \\r?\\n to split a string by new lines and return a stream; later, we trim and filter the empty lines. ... package com.mkyong.string.split; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class StringSplitNewLines { public static void main(String[] args) { StringBuilder sb = new StringBuilder(); sb.append("aaa \n"); sb.append("bbb \r\n"); sb.append("ccc\n"); sb.append("\n"); sb.append("ddd\r\n"); sb.append("\r\n"); sb.append("eee\n"); String text = sb.toString(); System.out.println("---Original---"); System.out.println(text); System.out.println("---Split---"); // split by new line, trim and filter empty line List<String> lines = Arrays.stream(text.split("\\r?\\n")) .map(x -> x.trim()) .filter(x -> x.length() > 0) .collect(Collectors.toList()); for (String line : lines) { System.out.println(line); } } }
🌐
Java67
java67.com › 2013 › 03 › how-to-split-string-in-java-regular-expression.html
How to Split String in Java using Regular Expression | Java67
For example, if we split String "First,Second,Third" on comma and provide limit as 2 then pattern will run one time, and split() will return String array with 2 Strings, "First" and "Second,Third". Since this method accepts a Java regular expression, it throws PatternSyntaxException, if the syntax of the regular expression is invalid.
🌐
Briebug
blog.briebug.com › home › articles › using the java string.split() method
Using the Java String.split() Method | Briebug
October 13, 2022 - It is a convenient method, provided by the Java String class to split a String into an Array of Strings, given the passed delimiter. There are two variants. One containing only the regex parameter and the other containing the regex and “limit”, ...
🌐
Stack Abuse
stackabuse.com › how-to-split-a-string-in-java
How to Split a String in Java
September 20, 2023 - Instead of this, we can use a regex character set This refers to putting the special characters to be escaped inside square brackets. This way, the special characters are treated as normal characters. For example, we could use a | as a delimiter by saying: ... The split() method of the Java String class is a very useful and often used tool.
🌐
TutorialsPoint
tutorialspoint.com › How-to-Split-String-in-Java-using-Regular-Expression
How to split a regular expression in Java
August 21, 2024 - 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 ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-split-strings-using-regular-expressions-in-java
How to Split Strings Using Regular Expressions in Java? - GeeksforGeeks
July 23, 2025 - First, we apply a String.split() on the given String and pass the given regex in the argument. It will return an Array of strings and we store it in the Array then we print the Array. ... // java program to split a string.
🌐
W3Resource
w3resource.com › java-tutorial › string › string_split.php
Java String: split Method - w3resource
August 19, 2022 - The following example shows the usage of java String() method. public class StringSplitRegex { public static void main(String[] args) { // Declare string object String StringValue = "John Gilbert:43:UK"; // define the regex parameter String regex = ":"; // split the string object String[]output = StringValue.split(regex); System.out.println(); // printing the array contents System.out.println("Name:"+output[0]); System.out.println("Age:"+output[1]); System.out.println("Country:"+output[2]); System.out.println(); } }
🌐
Baeldung
baeldung.com › home › java › java string › split a string in java and keep the delimiters
Split a String in Java and Keep the Delimiters | Baeldung
June 24, 2025 - We can also use a positive lookbehind assertion “((?<=@))” to split the string text: ... However, the resulting output still won’t contain the delimiters as individual elements of the array: ... We can use the combination of the above two explained look-arounds with a logical-or and see it in action. The resulting regex “((?=@)|(?<=@))” will definitely give us the desired results.
🌐
Baeldung
baeldung.com › home › java › java string › splitting a java string by multiple delimiters
Splitting a Java String by Multiple Delimiters | Baeldung
August 7, 2025 - They’re also a very popular solution when it comes to splitting a string. So, let’s see how we can use a regular expression to split a string by multiple delimiters in Java. First, we don’t need to add a new dependency since regular expressions are available in the java.util.regex package.
🌐
Sentry
sentry.io › sentry answers › java › how to split a string in java
Java String Split: Use String.split() With Regex | Sentry
September 23, 2026 - Split a string in Java using the String.split() method, which accepts a regex delimiter, requires escaping special characters, and handles edge case inputs
🌐
Medium
medium.com › sina-ahmadi › java-regex-6e4d073aab85
Java RegEx. special characters issue in Java split… | by Sina | My journey as a software developer | Medium
June 20, 2018 - Replace all special characters using Java’s “replaceAll” method in the input string, with escaped special characters ... escapedString = nonEscapedString.replaceAll("\\*", "\\\\*"); splittedString = escapedString.split("\\*")