1

I have some String objects that I need to split into an array of substrings at the locations of differing consecutive characters.

My Input/Output should look like this:

"AAAA"    -> ["AAAA"]
"AAAABBB" -> ["AAAA", "BBB"]
"ABBCCC"  -> ["A", "BB", "CCC"]

I want to be able to write a line of code like this:

String[] charRuns = str.split(regex);

Where str is the input and charRuns is the output, but what should the value of regex be?

4castle
  • 32,613
  • 11
  • 69
  • 106
  • 1
    [This question](http://stackoverflow.com/questions/15101577/split-string-when-character-changes-possible-regex-solution) might be of use – Oli Mar 18 '16 at 13:59
  • I wouldn't regex that... – Thomas Ayoub Mar 18 '16 at 14:01
  • @Thomas I will accept any solution that provides the input and output I need, I would just prefer a regex for simple code. – 4castle Mar 18 '16 at 14:04
  • @oli not as much as [the duplicate](http://stackoverflow.com/questions/13596454/split-regex-to-extract-strings-of-contiguous-characters) – Bohemian Mar 18 '16 at 14:22

2 Answers2

4

Can't find a way to do this with split yet, but here's a solution based on Pattern, Matcher and iteration:

String test = "ABBCCCDDDDE";
//                          | any character, grouped for back-reference
//                          | | immediate back-reference
//                          | |    | 0+ repetition, greedy
Pattern p = Pattern.compile("(.)\\1*");
Matcher m = p.matcher(test);
while (m.find()) System.out.println(m.group());

Output

A
BB
CCC
DDDD
E
Mena
  • 47,782
  • 11
  • 87
  • 106
  • This is an awesome solution! I will accept it once I'm sure that a regex doesn't exist for the `str.split` method. – 4castle Mar 18 '16 at 14:07
  • @4castle thanks! This solution **is** regex-based. But there's probably a way to pass the pattern to `String#split`, although you'd probably need to figure out a way with "look-around"s, as the `split` argument represents the separator, not the token. – Mena Mar 18 '16 at 14:08
  • @4castle this is a long-winded way of doing it. See the duplicate for [how to do it using `split()`](http://stackoverflow.com/a/13596720/256196) – Bohemian Mar 18 '16 at 14:16
  • @Bohemian Yes,its an exact duplicate. I'm closing this question then. – 4castle Mar 18 '16 at 14:25
0

There is really a simple way to do this without using Java regex at all, here is the pseudocode for it:

get the first character of the string, store it in variable firstChar.

count -> 1
startIndex -> 0
create a new arrayList to store the strings.
while(count <= string.length){
   newChar -> string.charAt(count)
   If(newChar != firstChar){
       arrayList.add(string.substring(startIndex, count)
       firstChar = newChar
       startIndex = count
     }
     increment count
  }

Once you have the data in arrayList you can iterate over it and create individual arrays.

rahsan
  • 170
  • 9