首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

正则替换的有关问题

2012-06-03 
正则替换的问题!string input htmlContentstring pattern @!--导航.start--[\s\S]!--导航.end--

正则替换的问题!
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); 

热点排行