Capture クラス
単一の部分式キャプチャの結果を表します。 Capture は、正常終了した単一のキャプチャで取得される 1 つの部分文字列を表します。
この型のすべてのメンバの一覧については、Capture メンバ を参照してください。
System.Object
System.Text.RegularExpressions.Capture
System.Text.RegularExpressions.Group
<Serializable>
Public Class Capture
[C#]
[Serializable]
public class Capture
[C++]
[Serializable]
public __gc class Capture
[JScript]
public
Serializable
class Capture
スレッドセーフ
この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。
解説
このオブジェクトは変更不可で、パブリック コンストラクタはありません。インスタンスは、 Captures によって返されたコレクションを通じて返されます。 Match クラスおよび Group クラスは Capture を拡張したクラスです。
使用例
Dim text As String = "One fish two fish red fish blue fish"
Dim pat As String = "(?<1>\w+)\s+(?<2>fish)\s*"
' Compile the regular expression.
Dim r As New Regex(pat, RegexOptions.IgnoreCase)
' Match the regular expression pattern against a text string.
Dim m As Match = r.Match(text)
While m.Success
' Display the first match and its capture set.
System.Console.WriteLine("Match=[" + m.ToString() + "]")
Dim cc As CaptureCollection = m.Captures
Dim c As Capture
For Each c In cc
System.Console.WriteLine("Capture=[" + c.ToString() + "]")
Next c
' Display Group1 and its capture set.
Dim g1 As Group = m.Groups(1)
System.Console.WriteLine("Group1=[" + g1.ToString() + "]")
Dim c1 As Capture
For Each c1 In g1.Captures
System.Console.WriteLine("Capture1=[" + c1.ToString() + "]")
Next c1
' Display Group2 and its capture set.
Dim g2 As Group = m.Groups(2)
System.Console.WriteLine("Group2=[" + g2.ToString() + "]")
Dim c2 As Capture
For Each c2 In g2.Captures
System.Console.WriteLine("Capture2=[" + c2.ToString() + "]")
Next c2
' Advance to the next match.
m = m.NextMatch()
End While
[C#]
string text = "One car red car blue car";
string pat = @"(?<1>\w+)\s+(?<2>car)\s*";
// 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);
while (m.Success)
{
// Display the first match and its capture set.
System.Console.WriteLine("Match=[" + m + "]");
CaptureCollection cc = m.Captures;
foreach (Capture c in cc)
{
System.Console.WriteLine("Capture=[" + c + "]");
}
// Display Group1 and its capture set.
Group g1 = m.Groups[1];
System.Console.WriteLine("Group1=[" + g1 + "]");
foreach (Capture c1 in g1.Captures)
{
System.Console.WriteLine("Capture1=[" + c1 + "]");
}
// Display Group2 and its capture set.
Group g2 = m.Groups[2];
System.Console.WriteLine("Group2=["+ g2 + "]");
foreach (Capture c2 in g2.Captures)
{
System.Console.WriteLine("Capture2=[" + c2 + "]");
}
// Advance to the next match.
m = m.NextMatch();
}
[C++]
String* text = S"One car red car blue car";
String* pat = S"(?<1>\\w+)\\s+(?<2>car)\\s*";
// 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);
while (m->Success)
{
// Display the first match and its capture set.
Console::WriteLine(S"Match=[{0}]", m);
CaptureCollection* cc = m->Captures;
{
Collections::IEnumerator* myEnum = cc->GetEnumerator();
while (myEnum->MoveNext())
{
Capture* c = __try_cast<Capture*>(myEnum->Current);
System::Console::WriteLine(S"Capture=[{0}]", c);
}
}
// display Group1 and its capture set.
Group* g1 = m->Groups->Item[1];
Console::WriteLine(S"Group1=[{0}]", g1);
{
Collections::IEnumerator* myEnum = g1->Captures->GetEnumerator();
while (myEnum->MoveNext())
{
Capture* c1 = __try_cast<Capture*>(myEnum->Current);
System::Console::WriteLine(S"Capture1=[{0}]", c1);
}
}
// display Group2 and its capture set.
Group* g2 = m->Groups->Item[2];
Console::WriteLine(S"Group2=[{0}]", g2);
{
Collections::IEnumerator* myEnum = g2->Captures->GetEnumerator();
while (myEnum->MoveNext())
{
Capture* c2 = __try_cast<Capture*>(myEnum->Current);
System::Console::WriteLine(S"Capture2=[{0}]", c2);
}
}
// advance to the next match.
m = m->NextMatch();
}
[JScript]
var text : String = "One car red car blue car";
var pat : String = "(?<1>\\w+)\\s+(?<2>fish)\\s*";
// Compile the regular expression.
var r : Regex = new Regex(pat, RegexOptions.IgnoreCase);
// match the regex pattern against a text string
var m : Match = r.Match(text);
while (m.Success) {
// Display the first match and its capture set.
System.Console.WriteLine("Match=[" + m + "]");
var cc : CaptureCollection = m.Captures;
for (var c : Capture in cc) {
System.Console.WriteLine("Capture=[" + c + "]");
}
// display Group1 and its capture set.
var g1 : Group = m.Groups[1];
System.Console.WriteLine("Group1=[" + g1 + "]");
for (var c1 : Capture in g1.Captures) {
System.Console.WriteLine("Capture1=[" + c1 + "]");
}
// display Group2 and its capture set.
var g2 : Group = m.Groups[2];
System.Console.WriteLine("Group2=["+ g2 + "]");
for (var c2 : Capture in g2.Captures) {
System.Console.WriteLine("Capture2=[" + c2 + "]");
}
// advance to the next match.
m = m.NextMatch();
}
必要条件
名前空間: System.Text.RegularExpressions
プラットフォーム: 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
アセンブリ: System (System.dll 内)