当前位置: 代码迷 >> ASP.NET >> Regex 正则 如何把斜杠打出来
  详细解决方案

Regex 正则 如何把斜杠打出来

热度:8061   发布时间:2013-02-25 00:00:00.0
Regex 正则 怎么把斜杠打出来
void Application_BeginRequest(object sender, EventArgs e)
  {
  Regex reg = new Regex(@".+News-(\d+)");

  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指向NewsCount.aspx?id=1

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

请问应该怎么改

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

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

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



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


------解决方案--------------------------------------------------------
或者在字符串前加一个@转义
------解决方案--------------------------------------------------------
C# code
 string input = "News-1"; string pattern = @"(?<=News)-(\d+)(?=\)"; input = System.Text.RegularExpressions.Regex.Replace(input, pattern, @"/$1");
------解决方案--------------------------------------------------------
探讨
C# code


string input = "News-1";
string pattern = @"(?<=News)-(\d+)(?=\)";
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+)");    HttpContext.Current.RewritePath(reg.Replace(HttpContext.Current.Request.Url.AbsolutePath, "News/$1"));}//或者是这样void Application_BeginRequest(object sender, EventArgs e){    Regex reg = new Regex(@"News-(\d+)");    HttpContext.Current.RewritePath(reg.Replace(HttpContext.Current.Request.Url.AbsolutePath, "News/$1"));}
------解决方案--------------------------------------------------------
整这么麻烦干嘛?
用伪静态,直接写在webconfig就行。
  相关解决方案