Match クラスは、正規表現の結果を格納するクラスです
確認した時点では、msdnのRegex.Match メソッドの説明は、翻訳が読みにくいので、自分で翻訳し直しました。
全ての部分を翻訳し直したわけではなく、また、リンクも再現していないので、元のページと比較しながらご利用下さい。
msdnの情報:Match クラス
一つの正規表現の一致の結果を表します。
一つの正規表現の一致の結果を表します。
構文
[SerializableAttribute]
public class Match : Group
プロパティ
Captures | 最も内側、最も左端を優先の順序で、キャプチャ・グループで、一致したすべてのキャプチャのコレクションを取得します。 (あるいは、最も内側、最も右側、優先の順序で、正規表現が、RegexOptions.RightToLeftオプションで修正される場合)。コレクションは、0以上のアイテムを持っている場合があります。(Group から継承されます。) |
Empty | 空のグループを取得します。すべての一致が失敗しすると、この空の一致を返します。 |
Groups | 正規表現に一致するグループのコレクションを取得します。 |
Index | キャプチャされた部分文字列の最初の文字が見つかった元の文字列の位置。(Capture から継承されます。) |
Length | キャプチャされた部分文字列の長さを取得します。(Capture から継承されます。) |
Name | 現在のインスタンスが表すキャプチャ・グループの名前を返します。(Group から継承されます。) |
Success | 一致が成功したかどうかを示す値を取得します。(Group から継承されます。) |
Value | 入力文字列から、キャプチャされた部分文字列を取得します。(Capture から継承されます。) |
メソッド
Equals(Object) | 指定されたオブジェクトが、現在のオブジェクトと同じかどうかを判断します。(Object から継承されます。) |
Finalize() | オブジェクトが、ガーベージ・コレクションによって回収される前に、リソースの解放を試みる、そして、他のクリーンアップ操作を実行します。(Object から継承されます。) |
GetHashCode() | 既定のハッシュ関数として機能します。(Object から継承されます。) |
GetType() | 現在のインスタンスのTypeを取得します。(Object から継承されます。) |
MemberwiseClone() | 現在のObjectの簡易コピーを作成します。(Object から継承されます。) |
NextMatch() | 最後の一致が終了した位置から開始する次の一致のために、結果と一緒に新しいMatchオブジェクトを返します。(最後に一致した文字の後の文字)。 |
Result(String) | 指定された置換パターンの展開部分を返します。 |
Synchronized(Match) | 複数のスレッドの間で共有するために適切な、提供されるものと等価なMatchインスタンスを返します。 |
ToString() | Valueプロパティを呼び出すことによって、入力文字列からキャプチャされた部分文字列を取得します。(Capture から継承されます。) |
備考
T:System.Text.RegularExpressions.Matchオブジェクトは不変であり、一般のコンストラクタを持っていません。T:System.Text.RegularExpressions.Matchクラスのインスタンスは、M:System.Text.RegularExpressions.Regex.Match(System.String)メソッドによって返され、文字列内の最初のパターン一致を表現します。後に続く一致は、M: System.Text.RegularExpressions.Match.NextMatchメソッドによって返される:System.Text.RegularExpressions.Matchオブジェクトで、表現されます。加えて、M:System.Text.RegularExpressions.Regex.Matches(System.String)メソッドによって、0、1やより多くのT:System.Text.RegularExpressions.Matchオブジェクトから構成されているT:System.Text.RegularExpressions.MatchCollectionオブジェクトが、返されます。
M:System.Text.RegularExpressions.Regex.Matches(System.String)メソッドが、入力文字列内の正規表現パターンとの一致が失敗する場合、それは、空のT:System.Text.RegularExpressions.MatchCollectionオブジェクトを返します。あなたは、続いて、コレクションを繰り返すために、C#のforeach構文やVisual BasicのFor Each構文を使用することができます。
M:System.Text.RegularExpressions.Regex.Match(System.String)メソッドが、正規表現パターンとの一致に失敗する場合、それは、P:System.Text.RegularExpressions.Match.Emptyと同じ、T:System.Text.RegularExpressions.Matchオブジェクトを返します。あなたは、一致が成功していたかどうか判断するために、P:System.Text.RegularExpressions.Group.Successプロパティを使用することができます。次の例は、図を提供します。
// Search for a pattern that is not found in the input string.
// 入力文字列内で見つからないパターンを探す
string pattern = "dog";
string input = "The cat saw the other cats playing in the back yard.";
Match match = Regex.Match(input, pattern);
if (match.Success )
// Report position as a one-based integer.
// 1から始まる整数で、位置を報告します。
Console.WriteLine("'{0}' was found at position {1} in '{2}'.",
match.Value, match.Index + 1, input);
else
Console.WriteLine("The pattern '{0}' was not found in '{1}'.",
pattern, input);
パターンの一致が成功している場合、P:System.Text.RegularExpressions.Capture.Valueプロパティには、一致した部分文字列が含まれています。P:System.Text.RegularExpressions.Capture.Indexプロパティは、入力文字列内で、一致した部分文字列の0から始まる開始位置を示します。そして、P:System.Text.RegularExpressions.Capture.Lengthプロパティは、入力文字列で一致する部分文字列の長さを示します。
一つの一致は、複数のキャプチャ・グループを含めることができるため、T:System.Text.RegularExpressions.Matchは、T:System.Text.RegularExpressions.GroupCollectionを返すP:System.Text.RegularExpressions.Match.Groupsプロパティを持っています。T:System.Text.RegularExpressions.Matchインスタンスそれ自体は、全体の一致を示すMatch.Groups[0](Visual BasicのMatch.Groups(0))で、コレクションの最初のオブジェクトと同等です。あなたは、次の方法で、一致でキャプチャされたグループにアクセスすることができます。:
- あなたは、foreach (C#)やFor Each (Visual Basic)構文を用いて、T:System.Text.RegularExpressions.GroupCollectionオブジェクトのメンバーを繰り返すことができます。
- あなたは、キャプチャ・グループの番号によって、グループを取得する:System.Text.RegularExpressions.GroupCollection.Item(System.Int32) propertyを使用することができます。インスタンスM:System.Text.RegularExpressions.Regex.GetGroupNumbersメソッドを呼び出すことによって、あなたが、正規表現内に、番号を付けたグループが存在するか判断できることに注意します。
- あなたは、キャプチャ・グループという名前で、グループを取得する:System.Text.RegularExpressions.GroupCollection.Item(System.String)プロパティを使用することができます。あなたが、M:System.Text.RegularExpressions.Regex.GetGroupNamesメソッドのインスタンスを呼び出すことによって、正規表現内に、名前の付いたグループが存在するか判断できることに注意します。
使用例
次の例は、正規表現Console\.Write(Line)?を使用します。正規表現は、次のように解釈されます。:
Console\.Write | 文字列の「Console.Write」に一致します。「.」文字がエスケープされることに注意します。そのため、それは、どんな文字にも一致するワイルドカードとしてよりもむしろ、リテラルのピリオドとして、解釈されます。 |
(Line)? | 文字列の「Line」の0、あるいは、1つの出現に一致します。 |
Example 1.
次の例は、入力文字列で、すべてのパターンの一致を取得するために、M:System.Text.RegularExpressions.Regex.Matches(System.String,System.String)メソッドを呼び出します。それは、続いて、各々の一致に関する情報を表示するために、返されたT:System.Text.RegularExpressions.MatchCollectionオブジェクト内で、T:System.Text.RegularExpressions.Matchオブジェクトを繰り返します。
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string input = "int[] values = { 1, 2, 3 };\n" +
"for (int ctr = values.GetLowerBound(1); ctr <= values.GetUpperBound(1); ctr++)\n" +
"{\n" +
" Console.Write(values[ctr]);\n" +
" if (ctr < values.GetUpperBound(1))\n" +
" Console.Write(\", \");\n" +
"}\n" +
"Console.WriteLine();\n";
string pattern = @"Console\.Write(Line)?";
MatchCollection matches = Regex.Matches(input, pattern);
foreach (Match match in matches)
Console.WriteLine("'{0}' found in the source code at position {1}.",
match.Value, match.Index);
}
}
// The example displays the following output:
// 'Console.Write' found in the source code at position 115.
// 'Console.Write' found in the source code at position 184.
// 'Console.WriteLine' found in the source code at position 211.
Example 2.
次の例は、一度に1つの一致を取得するために、M:System.Text.RegularExpressions.Regex.Match(System.String,System.String)とM:System.Text.RegularExpressions.Match.NextMatchメソッドを呼び出します。
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string input = "int[] values = { 1, 2, 3 };\n" +
"for (int ctr = values.GetLowerBound(1); ctr <= values.GetUpperBound(1); ctr++)\n" +
"{\n" +
" Console.Write(values[ctr]);\n" +
" if (ctr < values.GetUpperBound(1))\n" +
" Console.Write(\", \");\n" +
"}\n" +
"Console.WriteLine();\n";
string pattern = @"Console\.Write(Line)?";
Match match = Regex.Match(input, pattern);
while (match.Success)
{
Console.WriteLine("'{0}' found in the source code at position {1}.",
match.Value, match.Index);
match = match.NextMatch();
}
}
}
// The example displays the following output:
// 'Console.Write' found in the source code at position 115.
// 'Console.Write' found in the source code at position 184.
// 'Console.WriteLine' found in the source code at position 211.