Is it pretty enough? This is just one line ...
String parts = input.replaceAll("(.)(?!\\1)", "$1\0").split("\0");
Here's the test:
public static void main(String[] args) { String input = "1122333344555566778888"; String[] parts = input.replaceAll("(.)(?!\\1)", "$1\0").split("\0"); System.out.println(Arrays.toString(parts)); }
Output:
[11, 22, 3333, 44, 5555, 66, 77, 8888]
Please note that there is one very slight difficulty in this solution - the character after $1 in the replaceAll() call cannot be accessed in the input file. I chose the null character '\0' (i.e. hex zero) to be safe enough.
source share