December 2007
Intermediate to advanced
896 pages
19h 57m
English
You have a regular expression that contains one or more named groups, such as the following:
\\\\(?<TheServer>\w*)\\(?<TheService>\w*)\\
where the named group TheServer will match any server name within a UNC string, and TheService will match any service name within a UNC string.
This pattern does not match the UNCW format.
You need to store the groups that are returned by this regular expression in a keyed collection (such as a Dictionary<string, Group>) in which the key is the group name.
The ExtractGroupings method shown in Example 10-3 obtains a set of Group objects keyed by their matching group name.
Example 10-3. ExtractGroupings method
using System;
using System.Collections;
using System.Collections.Generics;
using System.Text.RegularExpressions;
public static List<Dictionary<string, Group>> ExtractGroupings(string source
string matchPattern,
bool wantInitialMatch)
{
List<Dictionary<string, Group>> keyedMatches =
new List<Dictionary<string, Group>>();
int startingElement = 1;
if (wantInitialMatch)
{
startingElement = 0;
}
Regex RE = new Regex(matchPattern, RegexOptions.Multiline);
MatchCollection theMatches = RE.Matches(source);
foreach(Match m in theMatches)
{
Dictionary<string, Group> groupings = new Dictionary<string, Group>();
for (int counter = startingElement; counter < m.Groups.Count; counter++) { // If we had just returned the MatchCollection directly, the // GroupNameFromNumber method would not be available ...Read now
Unlock full access