Split binary number into groups of zeros and ones

后端 未结 3 1662
离开以前
离开以前 2021-01-19 13:29

I have a binary number, for example 10000111000011, and want to split it into groups of consecutive 1s and 0s, 1 0000 111 0000 11.

I though

3条回答
  •  长情又很酷
    2021-01-19 14:06

    (?<=0)(?=1)|(?<=1)(?=0)
    

    Simply split by this.See demo.

    https://regex101.com/r/fM9lY3/3

    The lookarounds will find place where there is 0 behind and 1 ahead or 1 behind and 0 ahead.Thus resulting in correct split without consuming anything.

提交回复
热议问题