正則匹配:html
var reg = new System.Text.RegularExpressions.Regex(@"<article[\s\S]*?</article>"); _content = reg.Match(_htmlStr).Value;
正則替換:lua
Regex類有一個靜態的Replace方法,其實例也有一個Replace方法,這個方法很強大,由於它能夠傳入一個delegate,這樣,你能夠自定義每次捕獲匹配時,如何處理捕獲的內容。spa
static void Main(string[] args) { string s = "1 12 3 5"; s = Regex.Replace(s, @"\d+", new MatchEvaluator(CorrectString), RegexOptions.Compiled | RegexOptions.IgnoreCase); Console.WriteLine(s); Console.ReadLine(); } private static string CorrectString(Match match) { string matchValue = match.Value; if (matchValue.Length == 1) matchValue = "0" + matchValue; return matchValue; }