REGEX - Matching any character which repeats n times

前端 未结 4 1632
有刺的猬
有刺的猬 2021-01-01 19:45

How to match any character which repeats n times?

Example:

for input: abcdbcdcdd
for n=1:   ..........
for n=2:    .........
for n=3:            


        
4条回答
  •  不知归路
    2021-01-01 20:11

    With .NET regular expressions you can do following:

    (\w)(?<=(?=(?:.*\1){n})^.*) where n is variable
    

    Where:

    • (\w) — any character, captured in first group.
    • (?<=^.*) — lookbehind assertion, which return us to the start of the string.
    • (?=(?:.*\1){n}) — lookahead assertion, to see if string have n instances of that character.

    Demo

提交回复
热议问题