Capture.Value Capture.Value Capture.Value Capture.Value Property

定義

入力文字列からキャプチャした部分文字列を取得します。Gets the captured substring from the input string.

public:
 property System::String ^ Value { System::String ^ get(); };
public string Value { get; }
member this.Value : string
Public ReadOnly Property Value As String

プロパティ値

検索によってキャプチャされた部分文字列。The substring that is captured by the match.

次の例では、ピリオドを除く句読点を含まない文に一致する正規表現を定義します (".")。The following example defines a regular expression that matches sentences that contain no punctuation except for a period ("."). Match.Valueプロパティが一致した文では、各一致で構成される結果の文字列が表示されます。The Match.Value property displays the result string, which consists of a matched sentence, for each match. Group.Valueプロパティには、キャプチャ グループごとに結果の文字列が表示されます。 キャプチャ グループによってキャプチャされた最後の文字列で構成されています。The Group.Value property displays the result string for each capturing group; it consists of the last string captured by that capturing group. Capture.Valueプロパティは、各キャプチャの結果文字列を表示します。The Capture.Value property displays the result string for each capture.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "Yes. This dog is very friendly.";
      string pattern = @"((\w+)[\s.])+";
      foreach (Match match in Regex.Matches(input, pattern))
      {
         Console.WriteLine("Match: {0}", match.Value);
         for (int groupCtr = 0; groupCtr < match.Groups.Count; groupCtr++)
         {
            Group group = match.Groups[groupCtr];
            Console.WriteLine("   Group {0}: {1}", groupCtr, group.Value);
            for (int captureCtr = 0; captureCtr < group.Captures.Count; captureCtr++)
               Console.WriteLine("      Capture {0}: {1}", captureCtr, 
                                 group.Captures[captureCtr].Value);
         }                      
      }
   }
}
// The example displays the following output:
//       Match: Yes.
//          Group 0: Yes.
//             Capture 0: Yes.
//          Group 1: Yes.
//             Capture 0: Yes.
//          Group 2: Yes
//             Capture 0: Yes
//       Match: This dog is very friendly.
//          Group 0: This dog is very friendly.
//             Capture 0: This dog is very friendly.
//          Group 1: friendly.
//             Capture 0: This
//             Capture 1: dog
//             Capture 2: is
//             Capture 3: very
//             Capture 4: friendly.
//          Group 2: friendly
//             Capture 0: This
//             Capture 1: dog
//             Capture 2: is
//             Capture 3: very
//             Capture 4: friendly
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim input As String = "Yes. This dog is very friendly."
      Dim pattern As String = "((\w+)[\s.])+"
      For Each match As Match In Regex.Matches(input, pattern)
         Console.WriteLine("Match: {0}", match.Value)
         For groupCtr As Integer = 0 To match.Groups.Count - 1
            Dim group As Group = match.Groups(groupCtr)
            Console.WriteLine("   Group {0}: {1}", groupCtr, group.Value)
            For captureCtr As Integer = 0 To group.Captures.Count - 1
               Console.WriteLine("      Capture {0}: {1}", captureCtr, _
                                 group.Captures(captureCtr).Value)
            Next
         Next                      
      Next
   End Sub
End Module
' The example displays the following output:
'       Match: Yes.
'          Group 0: Yes.
'             Capture 0: Yes.
'          Group 1: Yes.
'             Capture 0: Yes.
'          Group 2: Yes
'             Capture 0: Yes
'       Match: This dog is very friendly.
'          Group 0: This dog is very friendly.
'             Capture 0: This dog is very friendly.
'          Group 1: friendly.
'             Capture 0: This
'             Capture 1: dog
'             Capture 2: is
'             Capture 3: very
'             Capture 4: friendly.
'          Group 2: friendly
'             Capture 0: This
'             Capture 1: dog
'             Capture 2: is
'             Capture 3: very
'             Capture 4: friendly

正規表現パターン ((\w+)[\s.])+ は、次の表に示すように定義されています。The regular expression pattern ((\w+)[\s.])+ is defined as shown in the following table. この正規表現、量指定子 (+) は正規表現全体に適用されますに注意してください。Note that in this regular expression, a quantifier (+) is applied to the entire regular expression.

パターンPattern 説明Description
(\w+) 1 つ以上の単語文字に一致します。Match one or more word characters. これが 2 番目のキャプチャ グループです。This is the second capturing group.
[\s.]) 一致の空白文字またはピリオド (".")。Match a white-space character or period (".").
((\w+)[\s.]) 空白文字またはピリオドが続く 1 つまたは複数の単語文字に一致 (".")。Match one or more word characters followed by a white-space character or period ("."). これが最初のキャプチャ グループです。This is the first capturing group.
((\w+)[\s.])+ 単語文字または文字の後に空白文字またはピリオドの 1 つまたは複数回の出現に一致 (".")。Match one or more occurrences of a word character or characters followed by a white-space character or period (".").

この例では 2 つの文の入力文字列で構成されます。In this example, the input string consists of two sentences. 最初の文から成る 1 つだけの word では、出力をそのため、CaptureCollectionオブジェクトには、1 つCaptureと同じキャプチャを表すオブジェクトをGroupオブジェクト。As the output shows, the first sentence consists of only one word, so the CaptureCollection object has a single Capture object that represents the same capture as the Group object. 2 番目の文は、複数の単語で構成されていますので、Groupオブジェクトには、最後の一致した部分式に関する情報にはのみが含まれています。The second sentence consists of multiple words, so the Group objects only contain information about the last matched subexpression. グループ 1、最初のキャプチャを表すには、期間は終了する文の最後の単語が含まれています。Group 1, which represents the first capture, contains the last word in the sentence that has a closing period. グループ 2、2 番目のキャプチャを表すには、対象の文の最後の単語が含まれています。Group 2, which represents the second capture, contains the last word in the sentence. ただし、CaptureグループのオブジェクトCaptureCollectionオブジェクトは、各一致文字列の部分式をキャプチャします。However, the Capture objects in the group's CaptureCollection object capture each subexpression match. Captureキャプチャの最初のキャプチャ グループのコレクション内のオブジェクトには、各キャプチャされた単語および空白文字または期間に関する情報が含まれています。The Capture objects in the first capturing group's collection of captures contain information about each captured word and white-space character or period. Captureキャプチャの 2 番目のキャプチャ グループのコレクション内のオブジェクトには、キャプチャされた各単語についての情報が含まれています。The Capture objects in the second capturing group's collection of captures contain information about each captured word.

次のコードの例では、正規表現パターンでは、^([a-z]+)(\d+)*\.([a-z]+(\d)*)$ピリオドで区切られた 2 つの部分で構成される製品番号が一致するようにします。The following example uses a regular expression pattern, ^([a-z]+)(\d+)*\.([a-z]+(\d)*)$, to match a product number that consists of two parts separated by a period. 両方の部分は、アルファベット文字の後に省略可能な番号で構成されます。Both parts consist of alphabetic characters followed by optional numbers. 最初の入力文字列がパターンを返された値と一致しないためSystem.Text.RegularExpressions.MatchオブジェクトのValueプロパティはString.Emptyします。Because the first input string does not match the pattern, the value of the returned System.Text.RegularExpressions.Match object's Value property is String.Empty. 同様に、正規表現パターンができない場合、キャプチャ グループに対応する値に一致するようにGroupオブジェクトのValueプロパティはString.Emptyします。Similarly, when the regular expression pattern is unable to match a capturing group, the value of the corresponding Group object's Value property is String.Empty.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      String pattern = @"^([a-z]+)(\d+)?\.([a-z]+(\d)*)$";
      String[] values = { "AC10", "Za203.CYM", "XYZ.CoA", "ABC.x170" };   
      foreach (var value in values) {
         Match m = Regex.Match(value, pattern, RegexOptions.IgnoreCase);
         if (m.Success) {
            Console.WriteLine("Match: '{0}'", m.Value);
            Console.WriteLine("   Number of Capturing Groups: {0}", 
                              m.Groups.Count);
            for (int gCtr = 0; gCtr < m.Groups.Count; gCtr++) {
               Group group = m.Groups[gCtr];
               Console.WriteLine("      Group {0}: {1}", gCtr, 
                                 group.Value == "" ? "<empty>" : "'" + group.Value + "'");   
               Console.WriteLine("         Number of Captures: {0}", 
                                 group.Captures.Count);
           
               for (int cCtr = 0; cCtr < group.Captures.Count; cCtr++) 
                  Console.WriteLine("            Capture {0}: {1}", 
                                    cCtr, group.Captures[cCtr].Value);
            }
         } 
         else {
            Console.WriteLine("No match for {0}: Match.Value is {1}", 
                              value, m.Value == String.Empty ? "<empty>" : m.Value);
         }
      }
   }
}
// The example displays the following output:
//       No match for AC10: Match.Value is <empty>
//       Match: 'Za203.CYM'
//          Number of Capturing Groups: 5
//             Group 0: 'Za203.CYM'
//                Number of Captures: 1
//                   Capture 0: Za203.CYM
//             Group 1: 'Za'
//                Number of Captures: 1
//                   Capture 0: Za
//             Group 2: '203'
//                Number of Captures: 1
//                   Capture 0: 203
//             Group 3: 'CYM'
//                Number of Captures: 1
//                   Capture 0: CYM
//             Group 4: <empty>
//                Number of Captures: 0
//       Match: 'XYZ.CoA'
//          Number of Capturing Groups: 5
//             Group 0: 'XYZ.CoA'
//                Number of Captures: 1
//                   Capture 0: XYZ.CoA
//             Group 1: 'XYZ'
//                Number of Captures: 1
//                   Capture 0: XYZ
//             Group 2: <empty>
//                Number of Captures: 0
//             Group 3: 'CoA'
//                Number of Captures: 1
//                   Capture 0: CoA
//             Group 4: <empty>
//                Number of Captures: 0
//       Match: 'ABC.x170'
//          Number of Capturing Groups: 5
//             Group 0: 'ABC.x170'
//                Number of Captures: 1
//                   Capture 0: ABC.x170
//             Group 1: 'ABC'
//                Number of Captures: 1
//                   Capture 0: ABC
//             Group 2: <empty>
//                Number of Captures: 0
//             Group 3: 'x170'
//                Number of Captures: 1
//                   Capture 0: x170
//             Group 4: '0'
//                Number of Captures: 3
//                   Capture 0: 1
//                   Capture 1: 7
//                   Capture 2: 0
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "^([a-z]+)(\d+)?\.([a-z]+(\d)*)$"
      Dim values() As String = { "AC10", "Za203.CYM", "XYZ.CoA", "ABC.x170" }   
      For Each value In values
         Dim m As Match = Regex.Match(value, pattern, RegexOptions.IgnoreCase)
         If m.Success Then
            Console.WriteLine("Match: '{0}'", m.Value)
            Console.WriteLine("   Number of Capturing Groups: {0}", 
                              m.Groups.Count)
            For gCtr As Integer = 0 To m.Groups.Count - 1
               Dim group As Group = m.Groups(gCtr)
               Console.WriteLine("      Group {0}: {1}", gCtr, 
                                 If(group.Value = "", "<empty>", "'" + group.Value + "'"))   
               Console.WriteLine("         Number of Captures: {0}", 
                                 group.Captures.Count)
           
               For cCtr As Integer = 0 To group.Captures.Count - 1
                  Console.WriteLine("            Capture {0}: {1}", 
                                    cCtr, group.Captures(cCtr).Value)
               Next
            Next
         Else
            Console.WriteLine("No match for {0}: Match.Value is {1}", 
                              value, If(m.Value = String.Empty, "<empty>", m.Value))
         End If
      Next    
   End Sub
End Module
' The example displays the following output:
'       No match for AC10: Match.Value is <empty>
'       Match: 'Za203.CYM'
'          Number of Capturing Groups: 5
'             Group 0: 'Za203.CYM'
'                Number of Captures: 1
'                   Capture 0: Za203.CYM
'             Group 1: 'Za'
'                Number of Captures: 1
'                   Capture 0: Za
'             Group 2: '203'
'                Number of Captures: 1
'                   Capture 0: 203
'             Group 3: 'CYM'
'                Number of Captures: 1
'                   Capture 0: CYM
'             Group 4: <empty>
'                Number of Captures: 0
'       Match: 'XYZ.CoA'
'          Number of Capturing Groups: 5
'             Group 0: 'XYZ.CoA'
'                Number of Captures: 1
'                   Capture 0: XYZ.CoA
'             Group 1: 'XYZ'
'                Number of Captures: 1
'                   Capture 0: XYZ
'             Group 2: <empty>
'                Number of Captures: 0
'             Group 3: 'CoA'
'                Number of Captures: 1
'                   Capture 0: CoA
'             Group 4: <empty>
'                Number of Captures: 0
'       Match: 'ABC.x170'
'          Number of Capturing Groups: 5
'             Group 0: 'ABC.x170'
'                Number of Captures: 1
'                   Capture 0: ABC.x170
'             Group 1: 'ABC'
'                Number of Captures: 1
'                   Capture 0: ABC
'             Group 2: <empty>
'                Number of Captures: 0
'             Group 3: 'x170'
'                Number of Captures: 1
'                   Capture 0: x170
'             Group 4: '0'
'                Number of Captures: 3
'                   Capture 0: 1
'                   Capture 1: 7
'                   Capture 2: 0

正規表現パターンは、次の表に示すように定義されます。The regular expression pattern is defined as shown in the following table:

パターンPattern 説明Description
^ 文字列の先頭から照合を開始します。Begin the match at the beginning of the string.
([a-z]+) 任意の文字の 1 つ以上の出現と一致する z。Match one or more occurrences of any character from a to z. 正規表現エンジンが渡されるため、RegexOptions.IgnoreCaseオプション、この比較は大文字です。Because the regular expression engine is passed the RegexOptions.IgnoreCase option, this comparison is case-insensitive. これが最初のキャプチャ グループです。This is the first capturing group.
(\d+)? 1 つ以上の 10 進数字の 0 個または 1 回を一致します。Match zero or one occurrence of one or more decimal digits. これが 2 番目のキャプチャ グループです。This is the second capturing group.
\. リテラルのピリオド文字と一致します。Match a literal period character.
([a-z]+ 任意の文字の 1 つ以上の出現と一致する z。Match one or more occurrences of any character from a to z. 比較では大文字です。The comparison is case-insensitive.
(\d)* 0 個以上の 10 進数と一致します。Match zero or more decimal digits. 一致した 1 桁の数字は、4 番目のキャプチャ グループです。A single matched digit is the fourth capturing group.
([a-z]+(\d)*) 1 つ以上のアルファベット文字と一致する z を 0、1、または複数の 10 進数字が続きます。Match one or more alphabetic characters from a to z followed by zero, one, or more decimal digits. これが 4 番目のキャプチャ グループです。This is the fourth capturing group.
$ 最後に、文字列の末尾で照合します。Conclude the match at the end of the string.

注釈

呼び出し、Regex.MatchまたはMatch.NextMatch、返された値の一致を検索するメソッドが失敗したMatch.ValueプロパティはString.Emptyします。If a call to the Regex.Match or Match.NextMatch method fails to find a match, the value of the returned Match.Value property is String.Empty. 場合は、正規表現エンジンは、キャプチャ グループと一致することです。If the regular expression engine is unable to match a capturing group. 返された値Group.ValueプロパティはString.Emptyします。the value of the returned Group.Value property is String.Empty. 説明については、2 番目の例を参照してください。See the second example for an illustration.

適用対象