日期:2014-05-17  浏览次数:20418 次

正则替换的问题!
string input = htmlContent;
  string pattern = @"<!--导航.start-->[\s\S]<!--导航.end-->";
  string replacement = "<!--导航.start-->{{导航}}<!--导航.end-->";
  Regex rgx = new Regex(pattern);
  htmlContent = rgx.Replace(input, replacement);

我想把htmlContent中从<!--导航.start-->到<!--导航.end-->替换成<!--导航.start-->{{导航}}<!--导航.end-->
帮我看看怎么写的?

------解决方案--------------------
string htmlContent = @"sfdddsdfsfd<!--导航.start-->4522122212112<!--导航.end-->sdfffffff<!--导航.start-->ddd<!--导航.end-->3232";

string pattern = @"<!--导航\.start-->(.*?)<!--导航\.end-->";
string replacement = "<!--导航.start-->{{导航}}<!--导航.end-->";

Regex rgx = new Regex(pattern);
htmlContent = rgx.Replace(htmlContent, replacement);

Response.Write(htmlContent);
------解决方案--------------------
string input = htmlContent;
string pattern = @"(?i)(?<=<!--导航.start-->)[\s\S]*?(?=<!--导航.end-->)";
string replacement = "{{导航}}";
Regex rgx = new Regex(pattern);
htmlContent = rgx.Replace(input, replacement);

------解决方案--------------------
C# code

string htmlContent = @"sfdddsdfsfd<!--导航.start-->4522122212112<!--导航.end-->sdfffffff<!--导航.start-->ddd<!--导航.end-->3232";

            string pattern = @"<!--导航\.start-->(.*?)<!--导航\.end-->";
            string replacement = "<!--导航.start-->{{导航}}<!--导航.end-->";

            Regex rgx = new Regex(pattern);
            htmlContent = rgx.Replace(htmlContent, replacement);