正则匹配、替换

正则匹配: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;
        } 
相关文章
相关标签/搜索