Split regex to extract Strings of contiguous characters

It is totally possible to write the regex for splitting in one step:

"(?<=(.))(?!\\1)"

Since you want to split between every group of same characters, we just need to look for the boundary between 2 groups. I achieve this by using a positive look-behind just to grab the previous character, and use a negative look-ahead and back-reference to check that the next character is not the same character.

As you can see, the regex is zero-width (only 2 look around assertions). No character is consumed by the regex.

Leave a Comment