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

Regex 正则 怎么把斜杠打出来
void Application_BeginRequest(object sender, EventArgs e)
  {
  Regex reg = new Regex(@".+News-(\d+).html");

  var match = reg.Match(HttpContext.Current.Request.Url.AbsolutePath);

  if (match.Success)
  {
  String id = match.Groups[1].Value;
   
  HttpContext.Current.RewritePath("NewsCount.aspx?id="+id);
  }
  }

在Global.asax中写的代码

如:News-1.html指向NewsCount.aspx?id=1

我现在想把News-1.html改成News/1.html

请问应该怎么改

我把@".+News-(\d+).html" 改成@".+News/(\d+).html" 不过不行

网上搜了一段时间 也没弄明白  

水平不行 还请高手指教一下 谢谢了



------解决方案--------------------
Regex reg = new Regex(".+News-(\\d+).html");


------解决方案--------------------
或者在字符串前加一个@转义
------解决方案--------------------
C# code

 string input = "News-1.html";
 string pattern = @"(?<=News)-(\d+)(?=\.html)";
 input = System.Text.RegularExpressions.Regex.Replace(input, pattern, @"/$1");

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


string input = "News-1.html";
string pattern = @"(?<=News)-(\d+)(?=\.html)";
input = System.Text.RegularExpressions.Regex.Replace(input, pattern, @"/$1");

------解决方案--------------------
try...

按你的需求
C# code
//要么是这样
void Application_BeginRequest(object sender, EventArgs e)
{
    Regex reg = new Regex(@".+News-(\d+).html");
    HttpContext.Current.RewritePath(reg.Replace(HttpContext.Current.Request.Url.AbsolutePath, "News/$1.html"));
}
//或者是这样
void Application_BeginRequest(object sender, EventArgs e)
{
    Regex reg = new Regex(@"News-(\d+).html");
    HttpContext.Current.RewritePath(reg.Replace(HttpContext.Current.Request.Url.AbsolutePath, "News/$1.html"));
}

------解决方案--------------------
整这么麻烦干嘛?
用伪静态,直接写在webconfig就行。