Match.Groups プロパティ

正規表現に一致したグループのコレクションを取得します。

Public Overridable ReadOnly Property Groups As GroupCollection
[C#]
public virtual GroupCollection Groups {get;}
[C++]
public: __property virtual GroupCollection* get_Groups();
[JScript]
public function get Groups() : GroupCollection;

プロパティ値

パターンに一致した文字グループ。

使用例

 
Dim text As String = "One car red car blue car"
Dim pat As String = "(\w+)\s+(car)"
' Compile the regular expression.
Dim r As Regex = new Regex(pat, RegexOptions.IgnoreCase)
' Match the regular expression pattern against a text string.
Dim m As Match = r.Match(text)
Dim matchcount as Integer = 0
While (m.Success)
   matchCount += 1
   Console.WriteLine("Match" & (matchCount))
   Dim i As Integer
   For i = 1 to 2
      Dim g as Group = m.Groups(i)
      Console.WriteLine("Group" & i & "='" & g.ToString() & "'")
      Dim cc As CaptureCollection = g.Captures
      Dim j As Integer 
      For j = 0 to cc.Count - 1
     Dim c As Capture = cc(j)
         Console.WriteLine("Capture" & j & "='" & c.ToString() _
            & "', Position=" & c.Index)
      Next j
   Next i
   m = m.NextMatch()
End While

[C#] 
string text = "One car red car blue car";
string pat = @"(\w+)\s+(car)";
// Compile the regular expression.
Regex r = new Regex(pat, RegexOptions.IgnoreCase);
// Match the regular expression pattern against a text string.
Match m = r.Match(text);
int matchCount = 0;
while (m.Success) 
{
   Console.WriteLine("Match"+ (++matchCount));
   for (int i = 1; i <= 2; i++) 
   {
      Group g = m.Groups[i];
      Console.WriteLine("Group"+i+"='" + g + "'");
      CaptureCollection cc = g.Captures;
      for (int j = 0; j < cc.Count; j++) 
      {
         Capture c = cc[j];
         System.Console.WriteLine("Capture"+j+"='" + c + "', Position="+c.Index);
      }
   }
   m = m.NextMatch();
}

[C++] 
String* text = S"One car red car blue car";
String* pat = S"(\\w+)\\s+(car)";
// Compile the regular expression.
Regex* r = new Regex(pat, RegexOptions::IgnoreCase);
// Match the regular expression pattern against a text string.
Match* m = r->Match(text);
int matchCount = 0;
while (m->Success) 
{
   Console::WriteLine(S"Match{0}", __box(++matchCount));
   for (int i = 1; i <= 2; i++) {
      Group* g = m->Groups->Item[i];
      Console::WriteLine(S"Group{0}='{1}'", __box(i), g);
      CaptureCollection* cc = g->Captures;
      for (int j = 0; j < cc->Count; j++) 
      {
         Capture* c = cc->Item[j];
         System::Console::WriteLine(S"Capture{0}='{1}', Position={2}", __box(j), c, __box(c->Index));
      }
   }
   m = m->NextMatch();
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

Match クラス | Match メンバ | System.Text.RegularExpressions 名前空間