string sentence = "The quick brown fox jumps over the lazy dog"; string word = "fox"; Match match = Regex.Match(sentence, word); if (match.Success) { Console.WriteLine("The first occurrence of '{0}' is found in '{1}' at position {2}.", word, sentence, match.Index); }
string url = "https://www.example.com/path/to/page?param1=value1¶m2=value2"; string pattern = @"^(?The System.Text.RegularExpressions namespace is part of the standard .NET Framework library, and does not require any additional packages or libraries.https?)://(? \w+(\.\w+)+)(? /\w*)\?(? [\w=&]+)$"; Match match = Regex.Match(url, pattern); if (match.Success) { string protocol = match.Groups["protocol"].Value; string domain = match.Groups["domain"].Value; string path = match.Groups["path"].Value; string query = match.Groups["query"].Value; Console.WriteLine("Protocol: {0}\nDomain: {1}\nPath: {2}\nQuerystring: {3}", protocol, domain, path, query); }