次の方法で共有


Group クラス

定義

1 つのキャプチャ グループからの結果を表します。

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
継承
Group
派生
属性

注釈

キャプチャ グループでは、量指定子のため、1 つの一致で 0 個、1 つ以上の文字列をキャプチャできます。 (詳細については、「 量指定子」を参照してください)。1 つのキャプチャ グループと一致するすべての部分文字列は、 Group.Captures プロパティから使用できます。 キャプチャされた最後の部分文字列に関する情報は、 Value および Index プロパティから直接アクセスできます。 (つまり、 Group インスタンスは、キャプチャ グループによって行われた最後のキャプチャを反映する、 Captures プロパティによって返されるコレクションの最後の項目と同じです)。

たとえば、Group オブジェクトと、Captures プロパティによって返されるSystem.Text.RegularExpressions.CaptureCollectionとの間のこの関係を明確にするのに役立ちます。 正規表現パターン (\b(\w+?)[,:;]?\s?)+[?.!] 文全体に一致します。 正規表現は、次の表に示すように定義されています。

パターン 説明
\b ワード境界から照合を開始します。
(\w+?) 1 つ以上 (ただし、できるだけ少ない文字数) の単語文字と一致します。 これは、2 番目の (内部) キャプチャ グループです。 (最初のキャプチャ グループには、 \b 言語要素が含まれます)。
[,:;]? コンマ、コロン、またはセミコロンの 0 回または 1 回の出現と一致します。
\s? 空白文字の 0 個または 1 回の出現と一致します。
(\b(\w+?)[,:;]?\s?)+ 単語の境界、1 つ以上の単語文字、句読点記号、および空白文字で構成されるパターンと 1 回以上一致します。 これが最初のキャプチャ グループです。
[?.!] ピリオド、疑問符、感嘆符の出現箇所と一致します。

この正規表現パターンでは、サブパターン (\w+?) は、文内の複数の単語と一致するように設計されています。 ただし、 Group オブジェクトの値はキャプチャ (\w+?) 最後の一致のみを表しますが、 Captures プロパティはキャプチャされたすべてのテキストを表す CaptureCollection を返します。 出力が示すように、2 番目のキャプチャ グループの CaptureCollection には 4 つのオブジェクトが含まれています。 これらの最後のオブジェクトは、 Group オブジェクトに対応します。

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 オプションで変更された場合は最も右の最初の順序)。 コレクションには、0 個以上の項目を含む場合があります。

Index

キャプチャされた部分文字列の最初の文字が見つかった元の文字列内の位置。

(継承元 Capture)
Length

キャプチャされた部分文字列の長さを取得します。

(継承元 Capture)
Name

現在のインスタンスによって表されるキャプチャ グループの名前を返します。

Success

一致が成功したかどうかを示す値を取得します。

Value

入力文字列からキャプチャされた部分文字列を取得します。

(継承元 Capture)
ValueSpan

入力文字列からキャプチャされたスパンを取得します。

(継承元 Capture)

メソッド

名前 説明
Equals(Object)

指定したオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Objectの簡易コピーを作成します。

(継承元 Object)
Synchronized(Group)

複数のスレッド間で安全に共有できる、指定されたものと同等の Group オブジェクトを返します。

ToString()

Value プロパティを呼び出して、入力文字列からキャプチャされた部分文字列を取得します。

(継承元 Capture)

適用対象