public GroupNumberFromName ( string name ) : int | ||
name | string | |
return | int |
string pattern = @"(?\w+)\s(? \d+)"; string input = "Hello 123"; Regex regex = new Regex(pattern); Match match = regex.Match(input); int groupNumber = regex.GroupNumberFromName("Number");
string pattern = @"(?In this example, we have a regex pattern that matches a date in the format of yyyy-mm-dd. We use the GroupNumberFromName method to retrieve the indices of the "Year", "Month", and "Day" groups and assign them to their respective variables. The package library for the System.Text.RegularExpressions namespace is part of the .NET Framework.\d{4})-(? \d{2})-(? \d{2})"; string input = "2021-06-08"; Regex regex = new Regex(pattern); Match match = regex.Match(input); int yearGroupNumber = regex.GroupNumberFromName("Year"); int monthGroupNumber = regex.GroupNumberFromName("Month"); int dayGroupNumber = regex.GroupNumberFromName("Day");