
The character "A" starts at index 4 here. Index: The index where the matched text begins within the input string. Length: This is the length of the Value string.

This is a substring of the original input. Value: This is the matched text, represented as a separate string. These describe the matched text (a substring of the input). A Match object, returned by Regex.Match has a Value, Length and Index. Parse: To parse the number, use int.Parse or int.TryParse on the Value here. To get further numbers, consider Matches() or NextMatch.ĭigits: We extract a group of digit characters and access the Value string representation of that number. A common requirement is extracting a number from a string. Static public string MatchKey(string input) / This returns the key that is matched within the input. This calls the static method specified.Ĭonsole.WriteLine(RegexUtil.MatchKey(input)) Static Class C# program that uses static Regex A Regex object does not help here.Ĭlass: Here a static class stores an instance Regex that can be used project-wide. Sometimes: We only need to call Match once in a program's execution. It can be shared throughout an entire project. For performance, we should usually use an instance object.

Match match = Regex.Match(input, Often a Regex instance object is faster than the static Regex.Match. String input = "/content/alternate-1.aspx" ToLower C# program that uses ToLower, Match I found using ToLower to normalize chars was a good choice. Sometimes we can preprocess strings before using Match() on them. Match match = Regex.Match(value, (match.Success) Return: NextMatch returns another Match object-it does not modify the current one. Two matches occur, so we use NextMatch to get the second one. Here: We match all the digits in the input string (4 and 5). We can call the NextMatch method to search for a match that comes after the current one in the text. Output alternate-1 Pattern This starts a verbatim string literal.Ĭontent/ The group must follow this string. Finally, we get the Group value and display it. Match match = Regex.Match(input, +)\.aspx$", String input = "/content/ alternate-1.aspx" Groups: This collection is indexed at 1, not zero-the first group is found at index 1. When true, a Match occurred and we can access its Value or Groups. Success: We test the result of Match with the Success property. It is also possible to call Match upon a Regex object. Static: We use the Regex.Match static method. We only accept ranges of characters and some punctuation. Here we match parts of a string (a file name in a directory path). Success: The returned Match object has a bool property called Success. Pattern: The Regex uses a pattern that indicates one or more digits. Namespace: All these types are found in the namespace. We use its constructor and the Match method, and then handle the returned Match object. Methods (like Match and Replace) are available.


A regular expression describes a text-based transformation.Ī class, Regex, handles regular expressions. In text, we often discover, and must process, textual patterns. This C# tutorial covers the Regex class and Regex.Match. C# Regex.Match Examples: Regular Expressions
