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.
Top answer 1 of 6
55
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.
2 of 6
20
str.split (" ")
res27: Array[java.lang.String] = Array(a, +, b, -, c, *, d, /, e, <, f, >, g, >=, h, <=, i, ==, j)
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); } } }
Videos
String.split() to Stream #java #shorts #coding #airhacks
03:11
Splitting string with pipe character | Split String Using Regex ...
04:23
Java: Split String at Delimiter | break String into Array of ...
03:31
Efficiently Using Regex in Java to Split Formatted Strings with ...
03:08
How to Use Java Regex to Split Data While Preserving Text in ...
01:40
How to Use Regex to Split Strings in Java Efficiently - YouTube
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.
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.util.regex.pattern.split
Pattern.Split Method (Java.Util.Regex) | Microsoft Learn
This method works as if by invoking the two-argument #split(java.lang.CharSequence, int) split method with the given input sequence and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array. The input "boo:and:foo", for example, yields the following results with these expressions: <table class="plain" style="margin-left:2em"> <caption style="display:none">Split examples showing regex and result</caption> <thead> <tr> <th scope="col">Regex</th> <th scope="col">Result</th> </tr> </thead> <tbody> <tr><th scope="row" style="text-weight:normal">:</th> <td>{ "boo", "and", "foo"}</td></tr> <tr><th scope="row" style="text-weight:normal">o</th> <td>{ "b", "", ":and:f"}</td></tr> </tbody> </table> Java documentation for java.util.regex.Pattern.split(java.lang.CharSequence).
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.
Tutorialspoint
tutorialspoint.com › java › lang › string_split.htm
Java - String split() Method
The Java String split() method is used to divide/split a string into an array of substrings. This method accepts a string representing a regular expression as a parameter, searches for the given pattern in the current string and, splits it at every
TutorialsPoint
tutorialspoint.com › javaregex › javaregex_pattern_split.htm
java.util.regex.Pattern.split() Method
The java.util.regex.Pattern.split(CharSequence input) method splits the given input sequence around matches of this pattern. Following is the declaration for java.util.regex.Pattern.split(CharSequence input) method. public String[] ...
Mkyong
mkyong.com › home › java › how to split a string in java
How to split a string in Java - Mkyong.com
February 9, 2022 - In Java, we can use String#split() to split a string. String phone = "012-3456789"; // String#split(string regex) accepts regex as the argument String[] output = phone.split("-"); String part1 = output[0]; // 012 String part2 = output[1]; // 3456789
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.
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.
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 ...
Top answer 1 of 2
1
This expression might be a little complicated, maybe we could start with:
.{1,10}^\s
DEMO
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final String regex = ".{1,10}^\\s";
final String string = "James has gone out for a meal.";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
RegEx Circuit
jex.im visualizes regular expressions:

2 of 2
1
First, remove all double spaces if exists and apply this regex.
.{1,11}(?:\s|$)|.{1,11}(?:[^\s]|$)
But I would use the split function and then 'for clause' calculating lengths.
W3Resource
w3resource.com › java-tutorial › string › string_split.php
Java String: split Method - w3resource
August 19, 2022 - Java String split Method: The split() method is used to split a given string around matches of the given regular expression.
Baeldung
baeldung.com › home › java › java string › split a string in java
Split a String in Java | Baeldung
January 8, 2024 - String[] splitted = input.trim().split("\\s*,\\s*"); Here, trim() method removes leading and trailing spaces in the input string, and the regex itself handles the extra spaces around delimiter. We can achieve the same result by using Java 8 Stream features: String[] splitted = Arrays.stream(input.split(",")) .map(String::trim) .toArray(String[]::new); We can split a string into two parts by getting its midpoint and using substring() to get the separated parts...
Coderanch
coderanch.com › t › 684973 › java › Split-string-list-string-regex
Split the string and get a list of string using regex (Features new in Java 8 forum at Coderanch)
The group() method returns a String, one only, so, after using that, you are going to get a one‑element Stream downstream from there. If however you do what you say you are doing, namely split the String, you can get a String[]: look at the return value for this method. You will of course require a different regex.