CaptureCollection クラス
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
1 つのキャプチャ グループによって作成されたキャプチャのセットを表します。 コレクションは不変 (読み取り専用) であり、パブリック コンストラクターがありません。
public ref class CaptureCollection : System::Collections::ICollection
public ref class CaptureCollection : System::Collections::Generic::ICollection<System::Text::RegularExpressions::Capture ^>, System::Collections::Generic::IEnumerable<System::Text::RegularExpressions::Capture ^>, System::Collections::Generic::IList<System::Text::RegularExpressions::Capture ^>, System::Collections::Generic::IReadOnlyCollection<System::Text::RegularExpressions::Capture ^>, System::Collections::Generic::IReadOnlyList<System::Text::RegularExpressions::Capture ^>, System::Collections::IList
public class CaptureCollection : System.Collections.ICollection
public class CaptureCollection : System.Collections.Generic.ICollection<System.Text.RegularExpressions.Capture>, System.Collections.Generic.IEnumerable<System.Text.RegularExpressions.Capture>, System.Collections.Generic.IList<System.Text.RegularExpressions.Capture>, System.Collections.Generic.IReadOnlyCollection<System.Text.RegularExpressions.Capture>, System.Collections.Generic.IReadOnlyList<System.Text.RegularExpressions.Capture>, System.Collections.IList
[System.Serializable]
public class CaptureCollection : System.Collections.ICollection
type CaptureCollection = class
interface ICollection
interface IEnumerable
type CaptureCollection = class
interface ICollection<Capture>
interface seq<Capture>
interface IEnumerable
interface IList<Capture>
interface IReadOnlyCollection<Capture>
interface IReadOnlyList<Capture>
interface ICollection
interface IList
type CaptureCollection = class
interface ICollection
interface IEnumerable
interface ICollection<Capture>
interface seq<Capture>
interface IList<Capture>
interface IReadOnlyCollection<Capture>
interface IReadOnlyList<Capture>
interface IList
[<System.Serializable>]
type CaptureCollection = class
interface ICollection
interface IEnumerable
Public Class CaptureCollection
Implements ICollection
Public Class CaptureCollection
Implements ICollection(Of Capture), IEnumerable(Of Capture), IList, IList(Of Capture), IReadOnlyCollection(Of Capture), IReadOnlyList(Of Capture)
- 継承
-
CaptureCollection
- 属性
- 実装
次の例では、Group.Captures プロパティと Match.Captures
プロパティによって返される CaptureCollection オブジェクト内の Capture オブジェクトを比較します。 また、Group オブジェクトと、Group.Captures プロパティによって返される CaptureCollection 内の Capture オブジェクトも比較します。 この例では、次の 2 つの正規表現を使用して、1 つの入力文字列内の一致を検索します。
\b\w+\W{1,2}
この正規表現パターンは、1 つ以上の単語文字で構成される単語を識別し、その後に空白や句読点などの 1 つまたは 2 つの単語以外の文字が続きます。 正規表現にはキャプチャ グループは含まれません。 この例の出力は、Match オブジェクトと、Group.Captures プロパティと
Match.Captures
プロパティによって返される CaptureCollection オブジェクトの両方に、同じ一致に関する情報が含まれていることを示しています。(\b\w+\W{1,2})+
この正規表現パターンは、文内の単語を識別します。 このパターンは、1 つ以上の単語文字の後に 1 つまたは 2 つの単語以外の文字が続く 1 つまたは 2 つのキャプチャ グループを定義します。 正規表現パターンでは、
+
量指定子を使用して、このグループの 1 つ以上の出現箇所と一致します。 この例の出力は、Match オブジェクトと、Match.Captures
プロパティによって返される CaptureCollection オブジェクトに、同じ一致に関する情報が含まれていることを示しています。 正規表現内の唯一のキャプチャ グループに対応する 2 番目の Group オブジェクトは、最後にキャプチャされた文字列のみを識別します。一方、最初のキャプチャ グループの Group.Captures プロパティによって返される CaptureCollection オブジェクトには、キャプチャされたすべての部分文字列が含まれます。
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern;
string input = "The young, hairy, and tall dog slowly walked across the yard.";
Match match;
// Match a word with a pattern that has no capturing groups.
pattern = @"\b\w+\W{1,2}";
match = Regex.Match(input, pattern);
Console.WriteLine("Pattern: " + pattern);
Console.WriteLine("Match: " + match.Value);
Console.WriteLine(" Match.Captures: {0}", match.Captures.Count);
for (int ctr = 0; ctr < match.Captures.Count; ctr++)
Console.WriteLine(" {0}: '{1}'", ctr, match.Captures[ctr].Value);
Console.WriteLine(" Match.Groups: {0}", match.Groups.Count);
for (int groupCtr = 0; groupCtr < match.Groups.Count; groupCtr++)
{
Console.WriteLine(" Group {0}: '{1}'",
groupCtr, match.Groups[groupCtr].Value);
Console.WriteLine(" Group({0}).Captures: {1}",
groupCtr, match.Groups[groupCtr].Captures.Count);
for (int captureCtr = 0; captureCtr < match.Groups[groupCtr].Captures.Count; captureCtr++)
Console.WriteLine(" Capture {0}: '{1}'",
captureCtr,
match.Groups[groupCtr].Captures[captureCtr].Value);
}
Console.WriteLine("-----\n");
// Match a sentence with a pattern that has a quantifier that
// applies to the entire group.
pattern = @"(\b\w+\W{1,2})+";
match = Regex.Match(input, pattern);
Console.WriteLine("Pattern: " + pattern);
Console.WriteLine("Match: " + match.Value);
Console.WriteLine(" Match.Captures: {0}", match.Captures.Count);
for (int ctr = 0; ctr < match.Captures.Count; ctr++)
Console.WriteLine(" {0}: '{1}'", ctr, match.Captures[ctr].Value);
Console.WriteLine(" Match.Groups: {0}", match.Groups.Count);
for (int groupCtr = 0; groupCtr < match.Groups.Count; groupCtr++)
{
Console.WriteLine(" Group {0}: '{1}'", groupCtr, match.Groups[groupCtr].Value);
Console.WriteLine(" Group({0}).Captures: {1}",
groupCtr, match.Groups[groupCtr].Captures.Count);
for (int captureCtr = 0; captureCtr < match.Groups[groupCtr].Captures.Count; captureCtr++)
Console.WriteLine(" Capture {0}: '{1}'", captureCtr, match.Groups[groupCtr].Captures[captureCtr].Value);
}
}
}
// The example displays the following output:
// Pattern: \b\w+\W{1,2}
// Match: The
// Match.Captures: 1
// 0: 'The '
// Match.Groups: 1
// Group 0: 'The '
// Group(0).Captures: 1
// Capture 0: 'The '
// -----
//
// Pattern: (\b\w+\W{1,2})+
// Match: The young, hairy, and tall dog slowly walked across the yard.
// Match.Captures: 1
// 0: 'The young, hairy, and tall dog slowly walked across the yard.'
// Match.Groups: 2
// Group 0: 'The young, hairy, and tall dog slowly walked across the yard.'
// Group(0).Captures: 1
// Capture 0: 'The young, hairy, and tall dog slowly walked across the yard.'
// Group 1: 'yard.'
// Group(1).Captures: 11
// Capture 0: 'The '
// Capture 1: 'young, '
// Capture 2: 'hairy, '
// Capture 3: 'and '
// Capture 4: 'tall '
// Capture 5: 'dog '
// Capture 6: 'slowly '
// Capture 7: 'walked '
// Capture 8: 'across '
// Capture 9: 'the '
// Capture 10: 'yard.'
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim pattern As String
Dim input As String = "The young, hairy, and tall dog slowly walked across the yard."
Dim match As Match
' Match a word with a pattern that has no capturing groups.
pattern = "\b\w+\W{1,2}"
match = Regex.Match(input, pattern)
Console.WriteLine("Pattern: " + pattern)
Console.WriteLine("Match: " + match.Value)
Console.WriteLine(" Match.Captures: {0}", match.Captures.Count)
For ctr As Integer = 0 To match.Captures.Count - 1
Console.WriteLine(" {0}: '{1}'", ctr, match.Captures(ctr).Value)
Next
Console.WriteLine(" Match.Groups: {0}", match.Groups.Count)
For groupCtr As Integer = 0 To match.Groups.Count - 1
Console.WriteLine(" Group {0}: '{1}'", groupCtr, match.Groups(groupCtr).Value)
Console.WriteLine(" Group({0}).Captures: {1}", _
groupCtr, match.Groups(groupCtr).Captures.Count)
For captureCtr As Integer = 0 To match.Groups(groupCtr).Captures.Count - 1
Console.WriteLine(" Capture {0}: '{1}'", _
captureCtr, _
match.Groups(groupCtr).Captures(captureCtr).Value)
Next
Next
Console.WriteLine("-----")
Console.WriteLine()
' Match a sentence with a pattern that has a quantifier that
' applies to the entire group.
pattern = "(\b\w+\W{1,2})+"
match = Regex.Match(input, pattern)
Console.WriteLine("Pattern: " + pattern)
Console.WriteLine("Match: " + match.Value)
Console.WriteLine(" Match.Captures: {0}", match.Captures.Count)
For ctr As Integer = 0 To match.Captures.Count - 1
Console.WriteLine(" {0}: '{1}'", ctr, match.Captures(ctr).Value)
Next
Console.WriteLine(" Match.Groups: {0}", match.Groups.Count)
For groupCtr As Integer = 0 To match.Groups.Count - 1
Console.WriteLine(" Group {0}: '{1}'", groupCtr, match.Groups(groupCtr).Value)
Console.WriteLine(" Group({0}).Captures: {1}", _
groupCtr, match.Groups(groupCtr).Captures.Count)
For captureCtr As Integer = 0 To match.Groups(groupCtr).Captures.Count - 1
Console.WriteLine(" Capture {0}: '{1}'", captureCtr, match.Groups(groupCtr).Captures(captureCtr).Value)
Next
Next
End Sub
End Module
' The example displays the following output:
' Pattern: \b\w+\W{1,2}
' Match: The
' Match.Captures: 1
' 0: 'The '
' Match.Groups: 1
' Group 0: 'The '
' Group(0).Captures: 1
' Capture 0: 'The '
' -----
'
' Pattern: (\b\w+\W{1,2})+
' Match: The young, hairy, and tall dog slowly walked across the yard.
' Match.Captures: 1
' 0: 'The young, hairy, and tall dog slowly walked across the yard.'
' Match.Groups: 2
' Group 0: 'The young, hairy, and tall dog slowly walked across the yard.'
' Group(0).Captures: 1
' Capture 0: 'The young, hairy, and tall dog slowly walked across the yard.'
' Group 1: 'yard.'
' Group(1).Captures: 11
' Capture 0: 'The '
' Capture 1: 'young, '
' Capture 2: 'hairy, '
' Capture 3: 'and '
' Capture 4: 'tall '
' Capture 5: 'dog '
' Capture 6: 'slowly '
' Capture 7: 'walked '
' Capture 8: 'across '
' Capture 9: 'the '
' Capture 10: 'yard.'
コレクションは不変 (読み取り専用) であり、パブリック コンストラクターがありません。 CaptureCollection オブジェクトには、1 つ以上の Capture オブジェクトが含まれています。
CaptureCollection クラスのインスタンスは、次のプロパティによって返されます。
Group.Captures プロパティ。 コレクションの各メンバーは、キャプチャ グループによってキャプチャされた部分文字列を表します。 量指定子がキャプチャ グループに適用されていない場合、CaptureCollection には、Group オブジェクトと同じキャプチャされた部分文字列を表す 1 つの Capture オブジェクトが含まれます。 量指定子がキャプチャ グループに適用されている場合、CaptureCollection にはキャプチャされた部分文字列ごとに 1 つの Capture オブジェクトが含まれ、Group オブジェクトは最後にキャプチャされた部分文字列についてのみ情報を提供します。
Match.Captures
プロパティ。 この場合、コレクションは、一致に関する情報を全体として提供する 1 つの Capture オブジェクトで構成されます。 つまり、CaptureCollection オブジェクトは、Match オブジェクトと同じ情報を提供します。
コレクションのメンバーを反復処理するには、GetEnumerator メソッドによって返される列挙子を取得する代わりに、言語によって提供されるコレクションイテレーション コンストラクト (C# での foreach
や Visual Basic での ...Next
の For Each
など) を使用する必要があります。
Count |
グループによってキャプチャされた部分文字列の数を取得します。 |
Is |
コレクションが読み取り専用かどうかを示す値を取得します。 |
Is |
コレクションへのアクセスが同期される (スレッド セーフ) かどうかを示す値を取得します。 |
Item[Int32] |
コレクションの個々のメンバーを取得します。 |
Sync |
コレクションへのアクセスを同期するために使用できるオブジェクトを取得します。 |
Copy |
コレクションのすべての要素を、指定したインデックスから始まる指定した配列にコピーします。 |
Copy |
|
Equals(Object) |
指定したオブジェクトが現在のオブジェクトと等しいかどうかを判断します。 (継承元 Object) |
Get |
コレクションを反復処理する列挙子を提供します。 |
Get |
既定のハッシュ関数として機能します。 (継承元 Object) |
Get |
現在のインスタンスの Type を取得します。 (継承元 Object) |
Memberwise |
現在の Objectの簡易コピーを作成します。 (継承元 Object) |
To |
現在のオブジェクトを表す文字列を返します。 (継承元 Object) |
製品 | バージョン |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1 |
UWP | 10.0 |
.NET に関するフィードバック
.NET はオープンソース プロジェクトです。 フィードバックを提供するにはリンクを選択します。