What is the regex to match a string not containing more than x consecutive characters

前端 未结 3 1815
栀梦
栀梦 2021-01-26 13:37

I want to match strings that do not contain more than 3 of the same character repeated in a row. So:

  • abaaaa [no match]
  • abawdasd [match]
  • abbbbasda
3条回答
  •  一个人的身影
    2021-01-26 14:19

    Use a negative lookahead with back references:

    ^(?:(.)(?!\1\1))*$
    

    See live demo using your examples.

    (.) captures each character in group 1 and the negative look ahead asserts that the next 2 chars are not repeats of the captured character.

提交回复
热议问题