本文整理匯總了C#中System.Text.RegularExpressions.Regex.GroupNameFromNumber方法的典型用法代碼示例。如果您正苦於以下問題:C# Regex.GroupNameFromNumber方法的具體用法?C# Regex.GroupNameFromNumber怎麽用?C# Regex.GroupNameFromNumber使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Text.RegularExpressions.Regex
的用法示例。
在下文中一共展示了Regex.GroupNameFromNumber方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Main
//引入命名空間
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"(?<city>[A-Za-z\s]+), (?<state>[A-Za-z]{2}) (?<zip>\d{5}(-\d{4})?)";
string[] cityLines = {"New York, NY 10003", "Brooklyn, NY 11238", "Detroit, MI 48204",
"San Francisco, CA 94109", "Seattle, WA 98109" };
Regex rgx = new Regex(pattern);
List<string> names = new List<string>();
int ctr = 1;
bool exitFlag = false;
// Get group names.
do {
string name = rgx.GroupNameFromNumber(ctr);
if (! String.IsNullOrEmpty(name))
{
ctr++;
names.Add(name);
}
else
{
exitFlag = true;
}
} while (! exitFlag);
foreach (string cityLine in cityLines)
{
Match match = rgx.Match(cityLine);
if (match.Success)
Console.WriteLine("Zip code {0} is in {1}, {2}.",
match.Groups[names[3]],
match.Groups[names[1]],
match.Groups[names[2]]);
}
}
}
輸出:
Zip code 10003 is in New York, NY. Zip code 11238 is in Brooklyn, NY. Zip code 48204 is in Detroit, MI. Zip code 94109 is in San Francisco, CA. Zip code 98109 is in Seattle, WA.