Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try this:</p> <pre><code>bool result = Regex.IsMatch(input, @".*(.+).*\1.*\1.*"); </code></pre> <p>Basically, it checks if a pattern of one or more characters appears 3 or more times in the same string. </p> <p><strong>Full explanation:</strong></p> <p>First, it matches 0 or more characters at the beginning of the string. Then it captures a group of one or more. Then it matches 0 or more, and then the group again. Then 0 or more <strong>again</strong>, and then the capture again. Then 0 or more again.</p> <p>If you require the string to be consecutive, try this:</p> <pre><code>bool result = Regex.IsMatch(input, @".*(.+)\1\1.*"); </code></pre> <p>Also, some performance test results:</p> <pre><code>Non-consecutive: 312ms Consecutive: 246ms </code></pre> <p>Tests were done with this program:</p> <pre><code>using System; using System.Diagnostics; using System.Text.RegularExpressions; class Program { public static void Main(string[] args) { string input = "brbrbr"; Regex one = new Regex(@".*(.+).*\1.*\1.*"); for (int i = 0; i &lt; 5; i++) { bool x = one.IsMatch(input); //warm regex up } Stopwatch sw = Stopwatch.StartNew(); for (int i = 0; i &lt; 100000; i++) { bool x = one.IsMatch(input); } sw.Stop(); Console.WriteLine("Non-consecutive: {0}ms", sw.ElapsedMilliseconds); Regex two = new Regex(@".*(.+)\1\1.*"); for (int i = 0; i &lt; 5; i++) { bool x = two.IsMatch(input); //warm regex up } Stopwatch sw2 = Stopwatch.StartNew(); for (int i = 0; i &lt; 100000; i++) { bool x = two.IsMatch(input); } sw.Stop(); Console.WriteLine("Consecutive: {0}ms", sw2.ElapsedMilliseconds); Console.ReadKey(true); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload