Capture.Value プロパティ

定義

入力文字列からキャプチャした部分文字列を取得します。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個以上の単語文字の後に空白文字またはピリオド (".") が続くパターンの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. 出力に示されているようにCaptureCollection 、最初の文は1つの単語で構成されているので、オブジェクトにはオブジェクトとGroup同じキャプチャを表す1つCaptureのオブジェクトがあります。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. ただし、グループCaptureCaptureCollectionオブジェクト内のオブジェクトは、各部分式の一致をキャプチャします。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. 2 Capture番目のキャプチャグループのキャプチャコレクション内のオブジェクトには、キャプチャされた各単語に関する情報が含まれています。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]+) A ~ z の任意の文字の1回以上の出現に一致します。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]+ A ~ z の任意の文字の1回以上の出現に一致します。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)*) A から z の後に0個、1個、または複数の10進数字が続く1つ以上のアルファベット文字と一致します。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.

注釈

メソッドまたはMatch.NextMatchメソッドのRegex.Match呼び出しが一致を見つけられなかった場合、返される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.

適用対象