Try the following:
bool result = Regex.IsMatch(input, @".*(.+).*\1.*\1.*");
Basically, it checks to see if the pattern of one or more characters 3 or more times on the same line.
Full explanation:
First, it matches 0 or more characters at the beginning of a line. Then it captures a group of one or more. Then it matches 0 or more, and then the group again. Then 0 or more again , and then capture again. Then again 0 or more.
If you want the string to be consistent, try the following:
bool result = Regex.IsMatch(input, @".*(.+)\1\1.*");
In addition, some performance test results:
Non-consecutive: 312ms Consecutive: 246ms
Tests were performed using this program:
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 < 5; i++) { bool x = one.IsMatch(input);
source share