I recently harnessed the power of a look-ahead regular expression to split a String:
"abc8".split("(?=\\d)|\\W")
If printed to the console this expression returns:
[abc, 8]
Very pleased with this result, I wanted to transfer this to Guava for further development, which looked like this:
Splitter.onPattern("(?=\\d)|\\W").split("abc8")
To my surprise the output changed to:
[abc]
Why?
You found a bug!
This is the method that
Splitter
uses to actually splitString
s (Splitter.SplittingIterator::computeNext
):The area of interest is:
This logic works great, unless the empty match happens at the end of a
String
. If the empty match does occur at the end of aString
, it will end up skipping that character. What this part should look like is (notice>=
->>
):