Regex to retrieve at least 5 same numbers in a row

前端 未结 2 566
时光取名叫无心
时光取名叫无心 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: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

提交回复
热议问题