当前位置:编程学习 > C#/ASP.NET >>

正则表达式问题

string requestedPath="/Web/Default.aspx?id=1123";
string lookFor="^/Web/Default.aspx?id=([0-9]*)$";
Regex re = new Regex(lookFor, RegexOptions.Compiled);
Match mc = re.Match(requestedPath);



string requestedPath="http://localhost:4465/Web/default.aspx?id=1234";
string lookFor="^http://localhost:4465/Web/Default.aspx?id=([0-9]*)$";
Regex re = new Regex(lookFor, RegexOptions.Compiled);
Match mc = re.Match(requestedPath);



大家帮忙看看上面这两个正则表达式为什么匹配不了啊,谢谢大家! --------------------编程问答--------------------

等待过客或者逍遥。 --------------------编程问答-------------------- --------------------编程问答-------------------- 这个有点小复杂··· --------------------编程问答-------------------- 正则表达式中?的问题 要转义
修改成这样:

(?i)^/Web/Default.aspx\?id=\d+$

(?i)^http://localhost:4465/Web/Default.aspx\?id=\d*$ --------------------编程问答-------------------- 没有转义

改成

string lookFor="^\/Web\/Default.aspx\?id=([0-9]*)$";
--------------------编程问答-------------------- string lookFor="^/Web/Default.aspx?id=([0-9]*)$";
改成
string lookFor="^/Web/Default.aspx\?id=(\d+)$"; --------------------编程问答--------------------

void Main()
{
string requestedPath="/Web/Default.aspx?id=1123";
string lookFor=@"(?i)^/Web/Default.aspx\?id=\d+$";
Regex re = new Regex(lookFor, RegexOptions.Compiled);
Match mc = re.Match(requestedPath);
Console.WriteLine(mc.Value);



string requestedPath1="http://localhost:4465/Web/default.aspx?id=1234";
string lookFor1=@"(?i)^http://localhost:4465/Web/Default.aspx\?id=\d*$";
Regex re1 = new Regex(lookFor1, RegexOptions.Compiled);
Match mc1 = re1.Match(requestedPath1);
Console.WriteLine(mc1.Value);
}

/*
结果:
/Web/Default.aspx?id=1123
http://localhost:4465/Web/default.aspx?id=1234

*/
--------------------编程问答-------------------- --------------------编程问答-------------------- 没有转义造成的,小数点和问号都要转义

但问题是,你以这种方式进行UrlRewrite?

另外,RegexOptions.Compiled不要滥用
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,