Looking to get a head start on your next software interview? Pickup a copy of the best book to prepare: Cracking The Coding Interview!
match() method to search for a pattern within a string. In C#, you can achieve the same functionality using the
Regex.Match() method takes two parameters: the string to search, and the regular expression pattern to match against. Here’s an example:
string input = "The quick brown fox jumps over the lazy dog.";
string pattern = "quick";
Match match = Regex.Match(input, pattern);
Console.WriteLine("Match found: " + match.Value);
In this example, we’re searching for the word “quick” within the input string. The
Match() method returns a
Match object, which contains information about the match. We can check if a match was found using the
Success property, and retrieve the matched string using the
You can also use regular expression patterns to search for more complex patterns within a string. For example, to search for all occurrences of a word that starts with the letter “q”, you could use the following pattern:
string pattern = @"\bq\w+";
\b character represents a word boundary, and the
\w+ pattern matches one or more word characters (letters, digits, or underscores).