简体   繁体   English

正则表达式来检测重复字符的重复

[英]Regex to detect repetitions of an speficif character

There's being a while since I last used regex... I have a string of 0s and 1s: 自从我上次使用正则表达式以来已经有一段时间了...我有一个0和1的字符串:

0010001110011000

And I have to split it by blocks of 1 (2 or more consecutive 1s): 我必须将其拆分为1(2个或更多连续的1)的块:

001000 # These are the blocks what matters to me
00
000

I'm wondering how to do that. 我想知道如何做到这一点。

I was trying something like this, in Python: 我在Python中尝试这样的事情:

l = re.compile("([1])\1+").split(s)

But it is not right. 但这是不对的。

Thank you. 谢谢。

You may use 您可以使用

re.split(r'1{2,}', s)

See the regex demo . 参见regex演示 Here, 1{2,} matches 2 or more (due to the limiting quantifier {2,} ) 1 digits. 在此, 1{2,}匹配2个或更多(由于限制量 {2,}1数字。

If the 1 s may appear at the beginning, you will need to remove empty elements, say, with filter(None, result) . 如果开始时可能出现1 s,则需要删除空元素,例如,使用filter(None, result)

See a Python demo . 参见Python演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM