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

前端 未结 3 1813
栀梦
栀梦 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:29

    I'm answering this question :

    Is there a regular expression for matching a string that has no more than 2 repeating characters?

    which was marked as an exact duplicate of this question.


    Its much quicker to negate the match instead

    if (!Regex.Match("hello world", @"(.)\1{2}").Success) Console.WriteLine("No dups");
    

提交回复
热议问题