本文整理汇总了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.