Bagikan melalui


Match Kelas

Definisi

Mewakili hasil dari satu kecocokan ekspresi reguler.

public ref class Match : System::Text::RegularExpressions::Group
public class Match : System.Text.RegularExpressions.Group
[System.Serializable]
public class Match : System.Text.RegularExpressions.Group
type Match = class
    inherit Group
[<System.Serializable>]
type Match = class
    inherit Group
Public Class Match
Inherits Group
Warisan
Atribut

Contoh

Contoh berikut menggunakan ekspresi Console\.Write(Line)?reguler . Ekspresi reguler ditafsirkan sebagai berikut:

Pola Deskripsi
Console\.Write Cocokkan string "Console.Write". (Karakter "." dilewati sehingga ditafsirkan sebagai periode harfiah daripada sebagai kartubebas yang cocok dengan karakter apa pun.)
(Line)? Cocokkan nol atau satu kemunculan string "Baris".

Contoh 1

Contoh berikut memanggil Regex.Matches(String, String) metode untuk mengambil semua kecocokan pola dalam string input. Kemudian iterasi Match objek dalam objek yang dikembalikan untuk menampilkan informasi tentang setiap kecocokan MatchCollection .

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 112.
//    'Console.Write' found in the source code at position 184.
//    'Console.WriteLine' found in the source code at position 207.
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim input As String = "Dim values() As Integer = { 1, 2, 3 }" & vbCrLf & _
                            "For ctr As Integer = values.GetLowerBound(1) To values.GetUpperBound(1)" & vbCrLf & _
                            "   Console.Write(values(ctr))" & vbCrLf & _
                            "   If ctr < values.GetUpperBound(1) Then Console.Write("", "")" & vbCrLf & _
                            "Next" & vbCrLf & _
                            "Console.WriteLine()"   
      Dim pattern As String = "Console\.Write(Line)?"
      Dim matches As MatchCollection = Regex.Matches(input, pattern)
      For Each match As Match In matches
         Console.WriteLine("'{0}' found in the source code at position {1}.", _ 
                           match.Value, match.Index)       
      Next                            
   End Sub
End Module
' 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.

Contoh 2

Contoh berikut memanggil Match(String, String) metode dan NextMatch untuk mengambil satu kecocokan sekaligus.

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 112.
//    'Console.Write' found in the source code at position 184.
//    'Console.WriteLine' found in the source code at position 207.
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim input As String = "Dim values() As Integer = { 1, 2, 3 }" & vbCrLf & _
                            "For ctr As Integer = values.GetLowerBound(1) To values.GetUpperBound(1)" & vbCrLf & _
                            "   Console.Write(values(ctr))" & vbCrLf & _
                            "   If ctr < values.GetUpperBound(1) Then Console.Write("", "")" & vbCrLf & _
                            "Next" & vbCrLf & _
                            "Console.WriteLine()"   
      Dim pattern As String = "Console\.Write(Line)?"
      Dim match As Match = Regex.Match(input, pattern)
      Do While match.Success
         Console.WriteLine("'{0}' found in the source code at position {1}.", _ 
                           match.Value, match.Index)
         match = match.NextMatch()                  
      Loop                            
   End Sub
End Module
' 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.

Keterangan

Objek Match tidak dapat diubah dan tidak memiliki konstruktor publik. Instans Match kelas dikembalikan oleh Regex.Match metode dan mewakili kecocokan pola pertama dalam string. Kecocokan berikutnya diwakili oleh Match objek yang dikembalikan oleh Match.NextMatch metode . Selain itu, MatchCollection objek yang terdiri dari nol, satu, atau beberapa Match objek dikembalikan oleh Regex.Matches metode .

Regex.Matches Jika metode gagal mencocokkan pola ekspresi reguler dalam string input, metode akan mengembalikan objek kosongMatchCollection. Anda kemudian dapat menggunakan foreach konstruksi di C# atau For Each konstruksi di Visual Basic untuk melakukan iterasi koleksi.

Regex.Match Jika metode gagal mencocokkan pola ekspresi reguler, metode mengembalikan Match objek yang sama dengan Match.Empty. Anda dapat menggunakan Success properti untuk menentukan apakah kecocokan berhasil. Contoh berikut memberikan ilustrasi.

// 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.
   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);
' Search for a pattern that is not found in the input string.
Dim pattern As String = "dog"
Dim input As String = "The cat saw the other cats playing in the back yard."
Dim match As Match = Regex.Match(input, pattern)
If match.Success Then
   ' Report position as a one-based integer.
   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)
End If

Jika kecocokan Value pola berhasil, properti berisi substring yang cocok, Index properti menunjukkan posisi awal berbasis nol dari substring yang cocok dalam string input, dan Length properti menunjukkan panjang substring yang cocok dalam string input.

Karena satu kecocokan dapat melibatkan beberapa grup pengambilan, Match memiliki Groups properti yang mengembalikan GroupCollection. Instans Match itu sendiri setara dengan objek pertama dalam koleksi, di Match.Groups[0] (Match.Groups(0) di Visual Basic), yang mewakili seluruh kecocokan. Anda dapat mengakses grup yang diambil dalam kecocokan dengan cara berikut:

  • Anda dapat mengurutkan anggota GroupCollection objek dengan menggunakan foreach konstruksi (C#) atau For Each (Visual Basic).

  • Anda dapat menggunakan GroupCollection.Item[Int32] properti untuk mengambil grup berdasarkan jumlah grup penangkapan. Perhatikan bahwa Anda dapat menentukan grup bernomor mana yang ada dalam ekspresi reguler dengan memanggil metode instans Regex.GetGroupNumbers .

  • Anda dapat menggunakan GroupCollection.Item[String] properti untuk mengambil grup berdasarkan nama grup penangkapan. Perhatikan bahwa Anda dapat menentukan grup bernama mana yang ada dalam ekspresi reguler dengan memanggil metode instans Regex.GetGroupNames() .

Properti

Nama Deskripsi
Captures

Mendapatkan koleksi semua tangkapan yang cocok dengan grup pengambilan, dalam urutan paling kiri-paling-pertama (atau urutan paling kanan-pertama dalam jika ekspresi reguler dimodifikasi dengan RightToLeft opsi ). Koleksi mungkin memiliki nol item atau lebih.

(Diperoleh dari Group)
Empty

Mendapatkan grup kosong. Semua kecocokan yang gagal mengembalikan kecocokan kosong ini.

Groups

Mendapatkan kumpulan grup yang cocok dengan ekspresi reguler.

Index

Posisi dalam string asli tempat karakter pertama substring yang diambil ditemukan.

(Diperoleh dari Capture)
Length

Mendapatkan panjang substring yang ditangkap.

(Diperoleh dari Capture)
Name

Mengembalikan nama grup penangkapan yang diwakili oleh instans saat ini.

(Diperoleh dari Group)
Success

Mendapatkan nilai yang menunjukkan apakah kecocokan berhasil.

(Diperoleh dari Group)
Value

Mendapatkan substring yang diambil dari string input.

(Diperoleh dari Capture)
ValueSpan

Mendapatkan rentang yang diambil dari string input.

(Diperoleh dari Capture)

Metode

Nama Deskripsi
Equals(Object)

Menentukan apakah objek yang ditentukan sama dengan objek saat ini.

(Diperoleh dari Object)
GetHashCode()

Berfungsi sebagai fungsi hash default.

(Diperoleh dari Object)
GetType()

Mendapatkan Type instans saat ini.

(Diperoleh dari Object)
MemberwiseClone()

Membuat salinan dangkal dari Objectsaat ini.

(Diperoleh dari Object)
NextMatch()

Mengembalikan objek baru Match dengan hasil untuk kecocokan berikutnya, dimulai pada posisi di mana kecocokan terakhir berakhir (pada karakter setelah karakter terakhir yang cocok).

Result(String)

Mengembalikan ekspansi pola penggantian yang ditentukan.

Synchronized(Match)

Mengembalikan instans yang Match setara dengan instans yang disediakan yang cocok untuk dibagikan di antara beberapa utas.

ToString()

Mengambil substring yang diambil dari string input dengan memanggil Value properti .

(Diperoleh dari Capture)

Berlaku untuk

Lihat juga