using System; using System.Text.RegularExpressions; public class Test { public static void Main() { string pattern = @"^((?(D)(?!))(?1)|(?(D)(?!))(?<-C>0)|(?(C)(?!))(?0)|(?(C)(?!))(?<-D>1))*(?(C)(?!))(?(D)(?!))$"; string[] matchedStrings = new[] { "1100", "1010", "0101", "11000011" }; string[] unmatchedStrings = new[] { "110", "001", "1100001" }; foreach (var s in matchedStrings) Console.WriteLine("Should matching {0}: {1}", s, Regex.IsMatch(s, pattern)); foreach (var s in unmatchedStrings) Console.WriteLine("Should not matching {0}: {1}", s, !Regex.IsMatch(s, pattern)); } }