Split string only if BOTH the negative lookahead and negative lookbehind are statisfied

Ste*_*oom 2 java regex

Hello i came along this question where the author wanted to convert the String:

exampleString =  "2 Marine Cargo       14,642 10,528  Denver Factory North     16,016 more text 8,609 argA 2,106 argB"
Run Code Online (Sandbox Code Playgroud)

into an array / list that looks similar to this:

String[] resultArray = {"2", "Marine Cargo", "14,642", "10,528", "Denver Factory North", "16,016",
                "more text", "8,609", "argA", "2,106", "argB"};
Run Code Online (Sandbox Code Playgroud)

So numeric parts (with or without a comma) are considered an element
and pure alpha sequences (divided by none, one or multiple spaces) are considered an element.

This can be done by matching the groups
or by splitting on the spaces where both the previous and the next part of the string is no alpha sequence. I am curious if the latter is possible. I think part should be done with a negative look ahead:

\s+(?![A-Za-z]+)
Run Code Online (Sandbox Code Playgroud)

and part with a negative look behind.

(?<![a-zA-Z])\s+
Run Code Online (Sandbox Code Playgroud)

I am looking to combine both statements in such a way that it only does not match if both the parts before and after the sequence of spaces are alpha, so you can chain multiple words together without splitting in between. I found another question on this topic but i am not able to reverse engineer it for this particular case. Is this possible?

Wik*_*żew 5

You may use

String[] results = exampleString.split("(?<=\\d)\\s+(?=[a-zA-Z])|(?<=[a-zA-Z])\\s+(?=\\d)|(?<=\\d)\\s+(?=\\d)");
Run Code Online (Sandbox Code Playgroud)

See the regex demo

Details

  • (?<=\d)\s+(?=[a-zA-Z]) - 1+ whitespaces that have a digit on the left and a letter on the right
  • | - or
  • (?<=[a-zA-Z])\s+(?=\d) - 1+ whitespaces that have a letter on the left and a digit on the right
  • | - or
  • (?<=\d)\s+(?=\d) - 1+ whitespaces that have a digit on the left and a digit on the right.

Java demo:

String exampleString =  "2 Marine Cargo       14,642 10,528  Denver Factory North     16,016 more text 8,609 argA 2,106 argB";
String results[] = exampleString.split("(?<=\\d)\\s+(?=[a-zA-Z])|(?<=[a-zA-Z])\\s+(?=\\d)|(?<=\\d)\\s+(?=\\d)");
for (String s: results) {
    System.out.println(s);
}
Run Code Online (Sandbox Code Playgroud)

Output:

2
Marine Cargo
14,642
10,528
Denver Factory North
16,016
more text
8,609
argA
2,106
argB
Run Code Online (Sandbox Code Playgroud)