using System; using System.Text.RegularExpressions; public class c1401 { public static void Main () { string uk_postcode = "([a-zA-Z][0-9][\\s][0-9][a-zA-Z][a-zA-Z])" + "|([a-zA-Z][0-9][0-9][\\s][0-9][a-zA-Z][a-zA-Z])" + "|([a-zA-Z][a-zA-Z][0-9][\\s][0-9][a-zA-Z])" + "|([a-zA-Z])([a-zA-Z][a-zA-Z][0-9][0-9]" + "[\\s][0-9][a-zA-Z][a-zA-Z])" + "|([a-zA-Z][0-9][a-zA-Z][\\s][0-9][a-zA-Z][a-zA-Z])" + "|([a-zA-Z][a-zA-Z][0-9][a-zA-Z][\\s][0-9][a-zA-Z][a-zA-Z])"; Regex rx = new Regex(uk_postcode, RegexOptions.Compiled | RegexOptions.IgnoreCase); string test_postcode; Console.WriteLine(" Type in a the post codes to test"); test_postcode = Console.ReadLine(); MatchCollection matches = rx.Matches(test_postcode); Console.WriteLine( "{0} match[es] found in string : {1}", matches.Count, test_postcode); foreach (Match match in matches) { GroupCollection groups = match.Groups; Console.WriteLine( " Valid UK post code found at position {0} ", groups[0].Index); } } }