
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Extracting Groups from a MatchCollection
|
563
returns a List<T> that contains Dictionary<string,Group> objects. These Diction-
ary<string,Group>
objects contain the matches for each of the named groups in the
regular expression, keyed by their group name.
This test method,
TestExtractGroupings, returns the following:
Key / Value = 0 / \\MyServer\MyService\
Key / Value = TheService / MyService
Key / Value = TheServer / MyServer
Key / Value = 0 / \\MyServer2\MyService2\
Key / Value = TheService / MyService2
Key / Value = TheServer / MyServer2
If the last parameter to the ExtractGroupings method were to be changed to false,
the following output would result:
Key / Value = TheService / MyService
Key / Value = TheServer / MyServer
Key / Value = TheService / MyService2
Key / Value = TheServer / MyServer2
The only difference between these two outputs are that the first grouping is not dis-
played when the last parameter to
ExtractGroupings is changed to false. The first
grouping is always the complete match of the regular expression.
Discussion
Groups within a regular expression can be defined in one of two ways. The first way
is to add parentheses around the subpattern that you wish to define as a grouping.
This type of grouping is sometimes labeled as unnamed. This grouping can later be
easily extracted ...