Regex.Replace Method
Regex.Replace 有十二種多載方法
一、string Replace(string input, string replacement)
using System;
using System.Text.RegularExpressions;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string str = "0123456789";
string pattern = "3";
Regex regex = new Regex(pattern);
string result = regex.Replace(str, "*");
Console.WriteLine(result);
}
}
}
結果為
二、string Replace(string input, string replacement, int count)
using System;
using System.Text.RegularExpressions;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string str = "01020304";
string pattern = @"0";
Regex regex = new Regex(pattern);
string result = regex.Replace(str, "*", 3);
Console.WriteLine(result);
}
}
}
結果為
說明:
count 參數表示最大取代次數。
三、string Replace(string input, string replacement, int count, int startat)
using System;
using System.Text.RegularExpressions;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string str = "0102030405";
string pattern = @"0";
Regex regex = new Regex(pattern);
string result = regex.Replace(str, "*", 3, 1);
Console.WriteLine(result);
}
}
}
結果為
說明:
startat 參數表示從第幾位置開始取代起。
四、string Replace(string input, MatchEvaluator evaluator)
using System;
using System.Text.RegularExpressions;
namespace ConsoleApp1
{
class Program
{
static string ToStar(Match m)
{
return "*";
}
static void Main(string[] args)
{
string str = "0102030405";
string pattern = @"0";
Regex regex = new Regex(pattern);
string result = regex.Replace(str, new MatchEvaluator(Program.ToStar));
Console.WriteLine(result);
}
}
}
結果為
說明:
evaluator 參數相等於委派函式,用來將符合條件者做加工再輸出。
五、string Replace(string input, MatchEvaluator evaluator, int count)
請參考先前例子
六、string Replace(string input, MatchEvaluator evaluator, int count, int startat)
請參考先前例子
七、static string Replace(string input, string pattern, string replacement)
using System;
using System.Text.RegularExpressions;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string str = "0123456789";
string pattern = "3";
string result = Regex.Replace(str, pattern, "*");
Console.WriteLine(result);
}
}
}
結果為
八、static string Replace(string input, string pattern, string replacement, RegexOptions options)
In a specified input string, replaces all strings that match a specified regular expression with a specified replacement string. Specified options modify the matching operation.
九、static string Replace(string input, string pattern, string replacement, RegexOptions options, TimeSpan matchTimeout)
In a specified input string, replaces all strings that match a specified regular expression with a specified replacement string. Additional parameters specify options that modify the matching operation and a time-out interval if no match is found.
十、static string Replace(string input, string pattern, MatchEvaluator evaluator)
請參考先前例子
十一、static string Replace(string input, string pattern, MatchEvaluator evaluator, RegexOptions options)
請參考先前例子
十二、static string Replace(string input, string pattern, MatchEvaluator evaluator, RegexOptions options, TimeSpan matchTimeout)
請參考先前例子
參考資料: