正则匹配、替换

正则匹配:

var reg = new System.Text.RegularExpressions.Regex(@"<article[\s\S]*?</article>");
_content = reg.Match(_htmlStr).Value;

 

正则替换:

Regex类有一个静态的Replace方法,其实例也有一个Replace方法,这个方法很强大,因为它可以传入一个delegate,这样,你可以自定义每次捕获匹配时,如何处理捕获的内容。

        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;
        } 

 

posted @ 2017-05-12 16:58  BeInNight  阅读(242)  评论(0编辑  收藏  举报