.Net 正則表達式替換

 

 

using System;

using System.Text.RegularExpressions;


namespace RegexTest

{

    public class RegexTest

    {

        public string Replace(Match m)

        {

            string f = @"<h2>{0}</h2>";

            return string.Format(f, m.Groups["replace"]);

        }


        public void Start()

        {

            string source = @"<h1>HEADER<h1>";


            Regex regex = new Regex(@"(/)(?[/w]*)(/)", 

                RegexOptions.IgnoreCase);

            source = regex.Replace(source, new MatchEvaluator(Replace));


            Console.WriteLine(source);

        }

    }

}

根據匹配替換一部分內容。
如將<h1>xxxx</h1>換爲<h2>xxxx</h2>
發佈了27 篇原創文章 · 獲贊 0 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章