Regex to retrieve at least 5 same numbers in a row

前端 未结 2 567
时光取名叫无心
时光取名叫无心 2021-01-29 16:48

Looking for the regexp for finding all longest digit sequences (starting with length = 5) of the same numbers. E.g.:

Input: \'hgfd 0022222233333 4444 5556555 000         


        
相关标签:
2条回答
  • 2021-01-29 17:08

    (\d)\1{5,} return ['222222', '0000000']

    (\d)\1{4,} return ['222222', '33333', '0000000']

    0 讨论(0)
  • 2021-01-29 17:14

    You could use the below regex to match all the longest digit sequences (starting with length = 5),

    (\d)\1{4,}
    

    DEMO

    >>> s = "hgfd 0022222233333 4444 5556555 0000000"
    >>> [x.group() for x in re.finditer(r"(\d)\1{4,}", s)]
    ['222222', '33333', '0000000']
    

    Update:

    (\d)(?:\s*\1\s*\1\s*\1)(?:\s*\1)+
    

    DEMO

    0 讨论(0)
提交回复
热议问题