fork(2) download
  1. // http://stackoverflow.com/q/33181434/5290909
  2. using System;
  3. using System.Text.RegularExpressions;
  4.  
  5. public class Test
  6. {
  7. public static void Main()
  8. {
  9. string input = "abcdbcdcdd";
  10.  
  11. // Loop all patterns
  12. for (int N = 2; N <= 5; N++)
  13. {
  14. string pattern = @"(.)(?<=(?=(?>.*?\1){" + N + @"})\A.*)";
  15.  
  16. Console.WriteLine("N = {0}\tPattern: {1}", N, pattern);
  17. Console.WriteLine("\tINPUT '{0}'", input);
  18.  
  19. MatchCollection matches = Regex.Matches(input, pattern);
  20.  
  21. // Loop all matches
  22. if (matches.Count > 0)
  23. {
  24. foreach (Match match in matches)
  25. {
  26. Console.WriteLine( "\tChar: '{0}' (pos: {1})", match.Value, match.Index );
  27. }
  28. }
  29. else
  30. {
  31. Console.WriteLine( "\tNo Matches!");
  32. }
  33. }
  34. }
  35. }
Success #stdin #stdout 0.12s 24880KB
stdin
Standard input is empty
stdout
N = 2	Pattern: (.)(?<=(?=(?>.*?\1){2})\A.*)
	INPUT 'abcdbcdcdd'
	Char: 'b' (pos: 1)
	Char: 'c' (pos: 2)
	Char: 'd' (pos: 3)
	Char: 'b' (pos: 4)
	Char: 'c' (pos: 5)
	Char: 'd' (pos: 6)
	Char: 'c' (pos: 7)
	Char: 'd' (pos: 8)
	Char: 'd' (pos: 9)
N = 3	Pattern: (.)(?<=(?=(?>.*?\1){3})\A.*)
	INPUT 'abcdbcdcdd'
	Char: 'c' (pos: 2)
	Char: 'd' (pos: 3)
	Char: 'c' (pos: 5)
	Char: 'd' (pos: 6)
	Char: 'c' (pos: 7)
	Char: 'd' (pos: 8)
	Char: 'd' (pos: 9)
N = 4	Pattern: (.)(?<=(?=(?>.*?\1){4})\A.*)
	INPUT 'abcdbcdcdd'
	Char: 'd' (pos: 3)
	Char: 'd' (pos: 6)
	Char: 'd' (pos: 8)
	Char: 'd' (pos: 9)
N = 5	Pattern: (.)(?<=(?=(?>.*?\1){5})\A.*)
	INPUT 'abcdbcdcdd'
	No Matches!