System.Text.RegularExpressions.Group Class

Represents the results from a single capturing group.

See Also: Group Members


public class Group : Capture


A capturing group can capture zero, one, or more strings in a single match because of quantifiers. (For more information, see Quantifiers.) All the substrings matched by a single capturing group are available from the Group.Captures property. Information about the last substring captured can be accessed directly from the Value and Index properties. (That is, the System.Text.RegularExpressions.Group instance is equivalent to the last item of the collection returned by the Group.Captures property, which reflects the last capture made by the capturing group.)

An example helps to clarify this relationship between a System.Text.RegularExpressions.Group object and the System.Text.RegularExpressions.CaptureCollection that is returned by the Group.Captures property. The regular expression pattern (\b(\w+?)[,:;]?\s?)+[?.!] matches entire sentences. The regular expression is defined as shown in the following table.


Begin the match at a word boundary.


Match one or more word characters, but as few characters as possible. This is the second (inner) capturing group. (The first capturing group includes the \b language element.)


Match zero or one occurrence of a comma, colon, or semicolon.


Match zero or one occurrence of a white-space character.


Match the pattern consisting of a word boundary, one or more word characters, a punctuation symbol, and a white-space character one or more times. This is the first capturing group.


Match any occurrence of a period, question mark, or exclamation point.

In this regular expression pattern, the subpattern (\w+?) is designed to match multiple words within a sentence. However, the value of the System.Text.RegularExpressions.Group object represents only the last match that (\w+?) captures, whereas the Group.Captures property returns a System.Text.RegularExpressions.CaptureCollection that represents all captured text. As the output shows, the System.Text.RegularExpressions.CaptureCollection for the second capturing group contains four objects. The last of these corresponds to the System.Text.RegularExpressions.Group object.

code reference: System.Text.RegularExpressions.Group.Class#1


Namespace: System.Text.RegularExpressions
Assembly: System (in System.dll)
Assembly Versions: 1.0.3300.0, 1.0.5000.0,,