Group 类
定义
表示来自单个捕获组的结果。Represents the results from a single capturing group.
public ref class Group : System::Text::RegularExpressions::Capture
public class Group : System.Text.RegularExpressions.Capture
[System.Serializable]
public class Group : System.Text.RegularExpressions.Capture
type Group = class
inherit Capture
[<System.Serializable>]
type Group = class
inherit Capture
Public Class Group
Inherits Capture
- 继承
- 派生
- 属性
注解
捕获组可以捕获单个匹配项中的零个、一个或多个字符串,因为限定符。A capturing group can capture zero, one, or more strings in a single match because of quantifiers. (有关详细信息,请参阅 限定符。 ) 由单个捕获组匹配的所有子字符串都可从 Group.Captures 属性获取。(For more information, see Quantifiers.) All the substrings matched by a single capturing group are available from the Group.Captures property. 可以从和属性中直接访问捕获的最后一个子字符串的相关信息 Value Index 。Information about the last substring captured can be accessed directly from the Value and Index properties. (即, Group 实例等效于属性返回的集合的最后一项 Captures ,这反映了捕获组进行的最后一次捕获。 ) (That is, the Group instance is equivalent to the last item of the collection returned by the Captures property, which reflects the last capture made by the capturing group.)
示例有助于澄清 Group 对象与属性返回的之间的关系 System.Text.RegularExpressions.CaptureCollection Captures 。An example helps to clarify this relationship between a Group object and the System.Text.RegularExpressions.CaptureCollection that is returned by the Captures property. 正则表达式模式 (\b(\w+?)[,:;]?\s?)+[?.!] 匹配整个句子。The regular expression pattern (\b(\w+?)[,:;]?\s?)+[?.!] matches entire sentences. 该正则表达式的定义如下表所示。The regular expression is defined as shown in the following table.
| 模式Pattern | 描述Description |
|---|---|
\b |
在单词边界处开始匹配。Begin the match at a word boundary. |
(\w+?) |
匹配一个或多个单词字符,但字符要尽可能的少。Match one or more word characters, but as few characters as possible. 这是第二 (内部) 捕获组。This is the second (inner) capturing group. (第一个捕获组包含 \b language 元素。 ) (The first capturing group includes the \b language element.) |
[,:;]? |
匹配一个逗号、冒号或分号的零个或一个匹配项。Match zero or one occurrence of a comma, colon, or semicolon. |
\s? |
匹配空白字符的零个或一个匹配项。Match zero or one occurrence of a white-space character. |
(\b(\w+?)[,:;]?\s?)+ |
匹配由单词边界、一个或多个单词字符、一个标点符号和一个空格字符组成的模式一次或多次。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. |
在此正则表达式模式中,子模式 (\w+?) 旨在匹配句子中的多个单词。In this regular expression pattern, the subpattern (\w+?) is designed to match multiple words within a sentence. 但是,对象的值 Group 只表示捕获的最后一个匹配项 (\w+?) ,而 Captures 属性返回一个 CaptureCollection 表示所有捕获的文本的。However, the value of the Group object represents only the last match that (\w+?) captures, whereas the Captures property returns a CaptureCollection that represents all captured text. 如输出所示, CaptureCollection 第二个捕获组的包含四个对象。As the output shows, the CaptureCollection for the second capturing group contains four objects. 其中最后一个对应于 Group 对象。The last of these corresponds to the Group object.
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"(\b(\w+?)[,:;]?\s?)+[?.!]";
string input = "This is one sentence. This is a second sentence.";
Match match = Regex.Match(input, pattern);
Console.WriteLine("Match: " + match.Value);
int groupCtr = 0;
foreach (Group group in match.Groups)
{
groupCtr++;
Console.WriteLine(" Group {0}: '{1}'", groupCtr, group.Value);
int captureCtr = 0;
foreach (Capture capture in group.Captures)
{
captureCtr++;
Console.WriteLine(" Capture {0}: '{1}'", captureCtr, capture.Value);
}
}
}
}
// The example displays the following output:
// Match: This is one sentence.
// Group 1: 'This is one sentence.'
// Capture 1: 'This is one sentence.'
// Group 2: 'sentence'
// Capture 1: 'This '
// Capture 2: 'is '
// Capture 3: 'one '
// Capture 4: 'sentence'
// Group 3: 'sentence'
// Capture 1: 'This'
// Capture 2: 'is'
// Capture 3: 'one'
// Capture 4: 'sentence'
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim pattern As String = "(\b(\w+?)[,:;]?\s?)+[?.!]"
Dim input As String = "This is one sentence. This is a second sentence."
Dim match As Match = Regex.Match(input, pattern)
Console.WriteLine("Match: " + match.Value)
Dim groupCtr As Integer = 0
For Each group As Group In match.Groups
groupCtr += 1
Console.WriteLine(" Group {0}: '{1}'", groupCtr, group.Value)
Dim captureCtr As Integer = 0
For Each capture As Capture In group.Captures
captureCtr += 1
Console.WriteLine(" Capture {0}: '{1}'", captureCtr, capture.Value)
Next
Next
End Sub
End Module
' The example displays the following output:
' Match: This is one sentence.
' Group 1: 'This is one sentence.'
' Capture 1: 'This is one sentence.'
' Group 2: 'sentence'
' Capture 1: 'This '
' Capture 2: 'is '
' Capture 3: 'one '
' Capture 4: 'sentence'
' Group 3: 'sentence'
' Capture 1: 'This'
' Capture 2: 'is'
' Capture 3: 'one'
' Capture 4: 'sentence'
属性
| Captures |
按从里到外、从左到右的顺序获取由捕获组匹配的所有捕获的集合(如果正则表达式用 RightToLeft 选项修改了,则顺序为按从里到外、从右到左)。Gets a collection of all the captures matched by the capturing group, in innermost-leftmost-first order (or innermost-rightmost-first order if the regular expression is modified with the RightToLeft option). 该集合可以有零个或更多的项。The collection may have zero or more items. |
| Index |
原始字符串中发现捕获的子字符串的第一个字符的位置。The position in the original string where the first character of the captured substring is found. (继承自 Capture) |
| Length |
获取捕获的子字符串的长度。Gets the length of the captured substring. (继承自 Capture) |
| Name |
返回由当前实例表示的捕获组的名称。Returns the name of the capturing group represented by the current instance. |
| Success |
获取一个值,该值指示匹配是否成功。Gets a value indicating whether the match is successful. |
| Value |
获取输入的字符串中的捕获的子字符串。Gets the captured substring from the input string. (继承自 Capture) |
方法
| Equals(Object) |
确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object. (继承自 Object) |
| GetHashCode() |
作为默认哈希函数。Serves as the default hash function. (继承自 Object) |
| GetType() |
获取当前实例的 Type。Gets the Type of the current instance. (继承自 Object) |
| MemberwiseClone() |
创建当前 Object 的浅表副本。Creates a shallow copy of the current Object. (继承自 Object) |
| Synchronized(Group) |
返回一个与提供的对象等效的 |
| ToString() |
通过调用 Value 属性,从输入的字符串中检索捕获的子字符串。Retrieves the captured substring from the input string by calling the Value property. (继承自 Capture) |