Can you use zero-width matching regex in String split?

Solution 1:

You need to take a look at zero width matching constructs:

(?=X)   X, via zero-width positive lookahead
(?!X)   X, via zero-width negative lookahead
(?<=X)  X, via zero-width positive lookbehind
(?<!X)  X, via zero-width negative lookbehind

Solution 2:

You can use \b (word boundary) as what to look for as it is zero-width and use that as the anchor for looking for < and >.

String s = "abc<def>ghi";
String[] bits = s.split("(?<=>)\\b|\\b(?=<)");
for (String bit : bits) {
  System.out.println(bit);
}

Output:

abc
<def>
ghi

Now that isn't a general solution. You will probably need to write a custom split method for that.

Your second example suggests it's not really split() you're after but a regex matching loop. For example:

String s = "Hello! Oh my!! Good bye!!";
Pattern p = Pattern.compile("(.*?!+)\\s*");
Matcher m = p.matcher(s);
while (m.find()) {
  System.out.println("[" + m.group(1) + "]");
}

Output:

[Hello!]
[Oh my!!]
[Good bye!!]