Sử dụng Regex.Replace để thay thế các phần khớp với mẫu regex. Ví dụ: thay thế mọi chuỗi khớp "B.t" (B, ký tự bất kỳ, t) bằng "BAT".
Ví dụ
using System;
using System.Text.RegularExpressions;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
string str = "Bit and Bat";
Console.WriteLine(str);
string res = Regex.Replace(str, "B.t", "BAT");
Console.WriteLine(res);
}
}
}
Kết quả
Bit and Bat BAT and BAT
Giải thích: Mẫu B.t khớp với "Bit" và "Bat". Cả hai đều được thay bằng "BAT".