MatchEvaluator Temsilci

Tanım

Bir yöntem işlemi sırasında her normal ifade eşleşmesi bulunduğunda Replace çağrılan yöntemi temsil eder.

C#
public delegate string MatchEvaluator(Match match);
C#
[System.Serializable]
public delegate string MatchEvaluator(Match match);

Parametreler

match
Match

Tek Match bir normal ifadeyi temsil eden nesne, yöntem Replace işlemi sırasında eşleşer.

Dönüş Değeri

Temsilci tarafından temsil edilen yöntemi tarafından MatchEvaluator döndürülen bir dize.

Öznitelikler

Örnekler

Aşağıdaki kod örneği, eşleşen her karakter grubunu eşleşme oluşumunun sayısıyla değiştirmek için temsilciyi kullanır MatchEvaluator .

C#
using System;
using System.Text.RegularExpressions;

class MyClass
{
   static void Main(string[] args)
   {
      string sInput, sRegex;

      // The string to search.
      sInput = "aabbccddeeffcccgghhcccciijjcccckkcc";

      // A very simple regular expression.
      sRegex = "cc";

      Regex r = new Regex(sRegex);
        
      MyClass c = new MyClass();

      // Assign the replace method to the MatchEvaluator delegate.
      MatchEvaluator myEvaluator = new MatchEvaluator(c.ReplaceCC);
        
      // Write out the original string.
      Console.WriteLine(sInput);

      // Replace matched characters using the delegate method.
      sInput = r.Replace(sInput, myEvaluator);
      
      // Write out the modified string.
      Console.WriteLine(sInput);
   }

   public string ReplaceCC(Match m)
   // Replace each Regex cc match with the number of the occurrence.
   {
      i++;
      return i.ToString() + i.ToString();		
   }
   public static int i=0;
}
// The example displays the following output:
//       aabbccddeeffcccgghhcccciijjcccckkcc
//       aabb11ddeeff22cgghh3344iijj5566kk77

Açıklamalar

gibi Regex.Replace(String, MatchEvaluator)bir MatchEvaluator değiştirme yöntemi tarafından bulunan her eşleşme için özel bir doğrulama veya düzenleme işlemi gerçekleştirmek üzere bir temsilci yöntemi kullanabilirsiniz. Eşleşen her dize için yöntemi, Replace eşleşmeyi MatchEvaluator temsil eden bir Match nesneyle temsilci yöntemini çağırır. Temsilci yöntemi, tercih ettiğiniz işlemeyi gerçekleştirir ve yöntemin Replace eşleşen dizeyle değiştirerek bir dize döndürür.

Uzantı Metotları

GetMethodInfo(Delegate)

Belirtilen temsilci tarafından temsil edilen yöntemi temsil eden bir nesnesi alır.

Şunlara uygulanır

Ürün Sürümler
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Ayrıca bkz.