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); } } }
java regex split
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
01:40
Using Regex for Java String Split: Extracting statement_1 and ...
03:11
Splitting string with pipe character | Split String Using Regex ...
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); } } }
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.
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.
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 - 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.