// From: https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex?view=netframework-4.7.2 // Simple example taken from above URL, and heavily adapted to use list of cities from Wikipedia using System; using System.Text.RegularExpressions; public class Test { public static void Main () { // Define a regular expression Regex rx1 = new Regex(@"^[0-9]+\s+([A-Za-z]*).*$", RegexOptions.Compiled); // test string to match against string text = @"2 Edinburgh 464,990 City City of Edinburgh"; // Find single match Match match1 = rx1.Match(text); // check and print if (match1.Success) { Console.WriteLine("First group '{0}' in matched sub-string '{1}' found in single line:\n{2}", match1.Groups[1], match1.Value, text); } else { Console.WriteLine("No match found in lines: {0}", text); } // Define a regular expression Regex rx = new Regex(@"[0-9]+\s+([A-Za-z]*).*\n", RegexOptions.Compiled); // Define a test string. string texts = @"Rank Locality Population Status Council area 1 Glasgow 599,650 City Glasgow City 2 Edinburgh 464,990 City City of Edinburgh 3 Aberdeen 196,670 City Aberdeen City 4 Dundee 147,710 City Dundee City 5 Paisley 76,220 Town Renfrewshire 6 East Kilbride 74,740 Town South Lanarkshire"; //Console.WriteLine("Multi-line string to match against:\n{0}", texts); // Find matches. MatchCollection matches = rx.Matches(texts); // Report the number of matches found. Console.WriteLine("{0} matches found in:\n {1}", matches.Count, texts); // Report on each match. foreach (Match match in matches) { GroupCollection groups = match.Groups; Console.WriteLine("Found city name '{0}' in line\n{1}", groups[1], match.Value); } } }