How to split a binary string into groups that containt only ones or zeros with Java regular expressions? [duplicate]

谁都会走 提交于 2021-01-28 05:44:10

问题


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);
            }
        }
    }
}

回答1:


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));
        }
    }

}



回答2:


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

(([01])\2*)

RegEx Demo




回答3:


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.




回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/34996244/how-to-split-a-binary-string-into-groups-that-containt-only-ones-or-zeros-with-j

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!