Capture.Value Properti

Definisi

Mendapatkan substring yang diambil dari string input.

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

Nilai Properti

Substring yang ditangkap oleh kecocokan.

Contoh

Contoh berikut mendefinisikan ekspresi reguler yang cocok dengan kalimat yang tidak berisi tanda baca kecuali untuk titik ("."). Properti Match.Value menampilkan string hasil, yang terdiri dari kalimat yang cocok, untuk setiap kecocokan. Properti Group.Value menampilkan string hasil untuk setiap grup penangkapan; ini terdiri dari string terakhir yang diambil oleh grup penangkapan tersebut. Properti Capture.Value menampilkan string hasil untuk setiap pengambilan.

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

Pola regex ((\w+)[\s.])+ didefinisikan seperti yang ditunjukkan pada tabel berikut. Perhatikan bahwa dalam ekspresi reguler ini, kuantifier (+) diterapkan ke seluruh ekspresi reguler.

Pola Deskripsi
(\w+) Cocokkan satu atau lebih karakter kata. Ini adalah grup pengambilan kedua.
[\s.]) Cocokkan karakter atau titik spasi putih (".").
((\w+)[\s.]) Cocokkan satu atau beberapa karakter kata diikuti dengan karakter atau titik spasi putih ("."). Ini adalah grup pengambilan pertama.
((\w+)[\s.])+ Cocokkan satu atau beberapa kemunculan karakter kata atau karakter diikuti dengan karakter spasi putih atau titik (".").

Dalam contoh ini, string input terdiri dari dua kalimat. Seperti yang ditunjukkan oleh output, kalimat pertama hanya terdiri dari satu kata, sehingga CaptureCollection objek memiliki satu Capture objek yang mewakili tangkapan yang sama dengan Group objek . Kalimat kedua terdiri dari beberapa kata, sehingga Group objek hanya berisi informasi tentang subekspresi terakhir yang cocok. Grup 1, yang mewakili pengambilan pertama, berisi kata terakhir dalam kalimat yang memiliki periode penutup. Grup 2, yang mewakili pengambilan kedua, berisi kata terakhir dalam kalimat. Namun, Capture objek dalam objek grup CaptureCollection menangkap setiap kecocokan subekspresi. Objek Capture dalam kumpulan pengambilan grup penangkapan pertama berisi informasi tentang setiap kata yang diambil dan karakter atau titik spasi putih. Objek Capture dalam kumpulan pengambilan grup penangkapan kedua berisi informasi tentang setiap kata yang diambil.

Contoh berikut menggunakan pola ekspresi reguler, ^([a-z]+)(\d+)*\.([a-z]+(\d)*)$, untuk mencocokkan nomor produk yang terdiri dari dua bagian yang dipisahkan oleh titik. Kedua bagian terdiri dari karakter alfabet diikuti dengan angka opsional. Karena string input pertama tidak cocok dengan pola, nilai properti objek Value yang dikembalikan System.Text.RegularExpressions.Match adalah String.Empty. Demikian pula, ketika pola ekspresi reguler tidak dapat mencocokkan grup penangkapan, nilai properti objek Value yang Group sesuai adalah 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

Pola ekspresi reguler didefinisikan seperti yang diperlihatkan dalam tabel berikut:

Pola Deskripsi
^ Mulailah pencocokan di awal string.
([a-z]+) Cocokkan satu atau beberapa kemunculan karakter apa pun dari a hingga z. Karena mesin ekspresi reguler melewati RegexOptions.IgnoreCase opsi , perbandingan ini tidak peka huruf besar/kecil. Ini adalah grup pengambilan pertama.
(\d+)? Cocokkan nol atau satu kemunculan satu atau beberapa digit desimal. Ini adalah grup pengambilan kedua.
\. Cocokkan karakter periode harfiah.
([a-z]+ Cocokkan satu atau beberapa kemunculan karakter apa pun dari a hingga z. Perbandingannya adalah tidak peka huruf besar/kecil.
(\d)* Cocokkan satu atau beberapa angka desimal. Satu digit yang cocok adalah grup penangkap keempat.
([a-z]+(\d)*) Cocokkan satu atau beberapa karakter alfabet dari a ke z diikuti oleh nol, satu, atau beberapa digit desimal. Ini adalah grup penangkapan keempat.
$ Akhiri kecocokan di akhir string.

Keterangan

Jika panggilan ke Regex.Match metode atau Match.NextMatch gagal menemukan kecocokan, nilai properti yang dikembalikan Match.Value adalah String.Empty. Jika mesin ekspresi reguler tidak dapat mencocokkan grup penangkap. nilai properti yang dikembalikan Group.Value adalah String.Empty. Lihat contoh kedua untuk ilustrasi.

Berlaku untuk