How to detect keyword in String without spaces?

Basically my desired outcome is to split a string based on known keywords regardless on if whitespace seperates the keyword. Below is an example of my current implementation, expect param String line = "sum:=5;":

private static String[] nextLineAsToken(String line) {
    return line.split("\\s+(?=(:=|<|>|=))");
}

Expected:

String[] {"sum", ":=", "5;"};

Actual:

String[] {"sum:=5;"};

I have a feeling this isn't possible, but it would be great to hear from you guys. Thanks.


Here is an example code that you can use to split your input into groups. White space characters like regular space are ignored. It is later printed to the output in for loop:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Example {
    public static void main(String[] args) {
        final String regex = "(\\w*)\\s*(:=)\\s*(\\d*;)";
        final String string = "sum:=5;";
        
        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));
            }
        }
    }
}

And this is the output:

Full match: sum:=5;
Group 1: sum
Group 2: :=
Group 3: 5;