Splitting a string with repeated characters into a list

后端 未结 3 1282
终归单人心
终归单人心 2020-11-28 13:31

I am not well experienced with Regex but I have been reading a lot about it. Assume there\'s a string s = \'111234\' I want a list with the string split into

3条回答
  •  旧时难觅i
    2020-11-28 13:57

    Use re.finditer():

    >>> s='111234'
    >>> [m.group(0) for m in re.finditer(r"(\d)\1*", s)]
    ['111', '2', '3', '4']
    

提交回复
热议问题