Is there a regex that would work with String.split()
to break a String into contiguous characters - ie split where the next character is different to the previous character?
Here's the test case:
String regex = "your answer here"; String[] parts = "aaabbcddeee".split(regex); System.out.println(Arrays.toString(parts));
Expected output:
[aaa, bb, c, dd, eee]
Although the test case has letters only as input, this is for clarity only; input characters may be any character.
Please do not provide "work-arounds" involving loops or other techniques.
The question is to find the right regex for the code as shown above - ie only using split()
and no other methods calls. It is not a question about finding code that will "do the job".
split(String regex) method splits this string around matches of the given regular expression. This method works in the same way as invoking the method i.e split(String regex, int limit) with the given expression and a limit argument of zero. Therefore, trailing empty strings are not included in the resulting array.
The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.
Split(String) Splits an input string into an array of substrings at the positions defined by a regular expression pattern specified in the Regex constructor.
split is faster, but complex separators which might involve look ahead, Regex is only option.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With