Match Sınıf
Tanım
Önemli
Bazı bilgiler ürünün ön sürümüyle ilgilidir ve sürüm öncesinde önemli değişiklikler yapılmış olabilir. Burada verilen bilgilerle ilgili olarak Microsoft açık veya zımni hiçbir garanti vermez.
Tek bir normal ifade eşleşmesinin sonuçlarını temsil eder.
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
- Devralma
- Öznitelikler
Örnekler
Aşağıdaki örneklerde normal ifadesi Console\.Write(Line)?kullanılır. Normal ifade aşağıdaki gibi yorumlanır:
| Desen | Açıklama |
|---|---|
Console\.Write |
"Console.Write" dizesini eşleştirin. ("." karakteri, herhangi bir karakterle eşleşen bir joker karakter yerine değişmez nokta olarak yorumlanır.) |
(Line)? |
"Line" dizesinin sıfır veya bir oluşumunu eşleştirin. |
Örnek 1
Aşağıdaki örnek, giriş dizesindeki Regex.Matches(String, String) tüm desen eşleşmelerini almak için yöntemini çağırır. Ardından döndürülen MatchCollection nesnedeki Match nesneleri yineler ve her eşleşme hakkında bilgi görüntüler.
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.
Örnek 2
Aşağıdaki örnek, bir kerede Match(String, String) bir eşleşme almak için ve NextMatch yöntemlerini çağırır.
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.
Açıklamalar
Match Nesne sabittir ve ortak oluşturucusunun olmaması. sınıfının bir örneği Match yöntemi tarafından Regex.Match döndürülür ve bir dizedeki ilk desen eşleşmesini temsil eder. Sonraki eşleşmeler yöntemi tarafından döndürülen nesnelerle Match.NextMatch temsil edilirMatch. Ayrıca, sıfır, bir veya daha fazla Match nesneden oluşan bir MatchCollection nesne yöntemi tarafından Regex.Matches döndürülür.
Regex.Matches Yöntem bir giriş dizesindeki normal ifade deseni ile eşleşmezse boş MatchCollection bir nesne döndürür. Daha sonra koleksiyonu yinelemek için C# içindeki bir foreach yapıyı veya Visual Basic'teki bir For Each yapıyı kullanabilirsiniz.
Regex.Match Yöntem normal ifade deseni ile eşleşmezse, öğesine eşit Match.Emptybir Match nesne döndürür. Eşleşmenin Success başarılı olup olmadığını belirlemek için özelliğini kullanabilirsiniz. Aşağıdaki örnek, bir gösterim sağlar.
// 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
Bir desen eşleşmesi başarılı olursa, Value özelliği eşleşen alt dizeyi içerir, Index özelliği giriş dizesinde eşleşen alt dizenin sıfır tabanlı başlangıç konumunu gösterir ve Length özellik de giriş dizesindeki eşleşen alt dizenin uzunluğunu gösterir.
Tek bir eşleşme birden çok yakalama grubu içerebileceğinden, Match öğesini döndüren GroupCollectionbir Groups özelliği vardır. Örneğin Match kendisi, koleksiyonun ilk nesnesine eşdeğerdir( Match.Groups[0]Match.Groups(0) Visual Basic'te), eşleşmenin tamamını temsil eder. Yakalanan gruplara bir eşleşmede aşağıdaki yollarla erişebilirsiniz:
(C#) veya
For Each(Visual Basic) yapısı kullanarakforeachnesnenin GroupCollection üyelerini yineleyebilirsiniz.Yakalama grubunun sayısına göre grupları almak için özelliğini kullanabilirsiniz GroupCollection.Item[Int32] . Örnek Regex.GetGroupNumbers yöntemini çağırarak normal ifadede hangi numaralandırılmış grupların mevcut olduğunu belirleyebileceğinizi unutmayın.
Yakalama grubunun adına göre grupları almak için özelliğini kullanabilirsiniz GroupCollection.Item[String] . Örnek Regex.GetGroupNames() yöntemini çağırarak normal ifadede hangi adlandırılmış grupların mevcut olduğunu belirleyebileceğinizi unutmayın.
Özellikler
| Name | Description |
|---|---|
| Captures |
Yakalama grubuyla eşleşen tüm yakalamaların en soldaki ilk sırada (veya normal ifade seçeneğiyle RightToLeft değiştirildiğinde en içteki en sağdaki ilk sırada) eşleşen tüm yakalamaların koleksiyonunu alır. Koleksiyonda sıfır veya daha fazla öğe olabilir. (Devralındığı yer: Group) |
| Empty |
Boş grubu alır. Tüm başarısız eşleşmeler bu boş eşleşmeyi döndürür. |
| Groups |
Normal ifadeyle eşleşen grup koleksiyonunu alır. |
| Index |
Yakalanan alt dizenin ilk karakterinin bulunduğu özgün dizedeki konum. (Devralındığı yer: Capture) |
| Length |
Yakalanan alt dizenin uzunluğunu alır. (Devralındığı yer: Capture) |
| Name |
Geçerli örnek tarafından temsil edilen yakalama grubunun adını döndürür. (Devralındığı yer: Group) |
| Success |
Eşleşmenin başarılı olup olmadığını gösteren bir değer alır. (Devralındığı yer: Group) |
| Value |
Yakalanan alt dizeyi giriş dizesinden alır. (Devralındığı yer: Capture) |
| ValueSpan |
Yakalanan yayılma süresini giriş dizesinden alır. (Devralındığı yer: Capture) |
Yöntemler
| Name | Description |
|---|---|
| Equals(Object) |
Belirtilen nesnenin geçerli nesneye eşit olup olmadığını belirler. (Devralındığı yer: Object) |
| GetHashCode() |
Varsayılan karma işlevi işlevi görür. (Devralındığı yer: Object) |
| GetType() |
Geçerli örneğin Type alır. (Devralındığı yer: Object) |
| MemberwiseClone() |
Geçerli Objectbasit bir kopyasını oluşturur. (Devralındığı yer: Object) |
| NextMatch() |
Son eşleşmenin sona erdiği konumdan (son eşleşen karakterden sonraki karakterde) başlayarak sonraki eşleşmenin sonuçlarını içeren yeni Match bir nesne döndürür. |
| Result(String) |
Belirtilen değiştirme deseninin genişlemesini döndürür. |
| Synchronized(Match) |
Birden çok iş parçacığı arasında paylaşmaya uygun sağlanan örnekle eşdeğer bir Match örnek döndürür. |
| ToString() |
Özelliğini çağırarak giriş dizesinden yakalanan alt dizeyi Value alır. (Devralındığı yer: Capture) |