2

I'm new to using regular expressions, but I think that in an instance like this using them would be the quickest and most ellegant way. I have a binary string, and I need to split it into groups that only contain consecutive zeros or ones, for example:

110001
would be split into 
11
000
1

I just can't figure it out, this is my current code, thanks:

class Solution {    
    public static void main(String args[]) {          
            String binary = Integer.toBinaryString(67);
            String[] exploded = binary.split("0+| 1+");                

            for(String string : exploded) {
                System.out.println(string);
            }
        }
    }
}
masm64
  • 1,222
  • 3
  • 14
  • 31

5 Answers5

3

Try

public class Solution {

    public static void main(String[] args) {
        String binary = Integer.toBinaryString(67); 
        System.out.println(binary);
        Pattern pattern = Pattern.compile("0+|1+");
        Matcher matcher = pattern.matcher(binary);
        while (matcher.find()) {
            System.out.println(matcher.group(0));
        }
    }

}
Johannes Flügel
  • 3,112
  • 3
  • 17
  • 32
2

Rather than split you can use match using this regex and use captured group #1 for your matches:

(([01])\2*)

RegEx Demo

anubhava
  • 761,203
  • 64
  • 569
  • 643
1

if you want to use split (?<=0)(?=1)|(?<=1)(?=0).
Not sure what you want to do with all zero's or all one's though.

1

The method split requires a pattern to describe the separator. To achieve you goal you have to describe a location (you don’t want to consume characters at the split position) between the groups:

public static void main(String args[]) {          
    String binary = Integer.toBinaryString(67);
    String[] exploded = binary.split("(?<=([01]))(?!\\1)");

    for(String string : exploded) {
        System.out.println(string);
    }
}

(?<=([01])) describes via “look-behind” that before the splitting position, there must be either 1 or 0, and captures the character in a group. (?!\\1) specifies via “negative look-ahead” that the character after the split position must be different than the character found before the position.

Which is exactly what is needed to split into groups of the same character. You could replace [01] with . here to make it a general solution for splitting into groups having the same character, regardless of which one.

Holger
  • 285,553
  • 42
  • 434
  • 765
0

The reason it's not working is because of the nature of the split method. The found pattern will not be included in the array. You would need to use a regex search instead.

Matthew Chrobak
  • 384
  • 2
  • 8