Looks like you can just split on \b
in this case ("\\b"
as a string literal).
Generally you want to split on zero-width matching constructs, which \b
is, but also lookarounds can be used.
Related questions
Splitting based on a custom word boundary
If \b
isn't fitting your definition, you can always define your own boundaries using assertions.
For example, the following regex splits on the boundary between a meta character class X
and its complement
(?=[X])(?<=[^X])|(?=[^X])(?<=[X])
In the following example, we define X
to be \d
:
System.out.println(java.util.Arrays.toString(
"007james123bond".split(
"(?=[X])(?<=[^X])|(?=[^X])(?<=[X])".replace("X", "\\d")
)
)); // prints "[007, james, 123, bond]"
Here's another example where X
is a-z$
:
System.out.println(java.util.Arrays.toString(
"$dollar . . blah-blah $more gimme".split(
"(?=[X])(?<=[^X])|(?=[^X])(?<=[X])".replace("X", "a-z$")
)
)); // prints "[$dollar, . . , blah, -, blah, , $more, , gimme]"