Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string into repeated characters

Tags:

I want to split the string "aaaabbbccccaaddddcfggghhhh" into "aaaa", "bbb", "cccc". "aa", "dddd", "c", "f" and so on.

I tried this:

String[] arr = "aaaabbbccccaaddddcfggghhhh".split("(.)(?!\\1)"); 

But this eats away one character, so with the above regular expression I get "aaa" while I want it to be "aaaa" as the first string.

How do I achieve this?

like image 985
Lokesh Avatar asked May 07 '14 16:05

Lokesh


People also ask

How do you make a string repeating characters?

Java has a repeat function to build copies of a source string: String newString = "a". repeat(N); assertEquals(EXPECTED_STRING, newString);

How do you split a string into characters?

Split(Char, Int32, StringSplitOptions) Splits a string into a maximum number of substrings based on a specified delimiting character and, optionally, options. Splits a string into a maximum number of substrings based on the provided character separator, optionally omitting empty substrings from the result.

How do I split a string into multiple parts?

Answer: You just have to pass (“”) in the regEx section of the Java Split() method. This will split the entire String into individual characters.


2 Answers

Try this:

String   str = "aaaabbbccccaaddddcfggghhhh"; String[] out = str.split("(?<=(.))(?!\\1)");  System.out.println(Arrays.toString(out)); => [aaaa, bbb, cccc, aa, dddd, c, f, ggg, hhhh] 

Explanation: we want to split the string at groups of same chars, so we need to find out the "boundary" between each group. I'm using Java's syntax for positive look-behind to pick the previous char and then a negative look-ahead with a back reference to verify that the next char is not the same as the previous one. No characters were actually consumed, because only two look-around assertions were used (that is, the regular expresion is zero-width).

like image 103
Óscar López Avatar answered Sep 22 '22 09:09

Óscar López


What about capturing in a lookbehind?

(?<=(.))(?!\1|$) 

as a Java string:

(?<=(.))(?!\\1|$) 
like image 44
Jonny 5 Avatar answered Sep 25 '22 09:09

Jonny 5