我正在寫使用.NET的正則表達式引擎以下正則表達式:URL重寫正則表達式與捕獲組
Regex reg = new Regex(@"/Explorer/PeopleDirectory([/\?].*)?$");
var a = reg.Replace(@"http://localhost:106/Explorer/PeopleDirectory/assets/images/logo.png", "/Explorer/PeopleDirectoryProxy.aspx?pdurl=$0");
var b = reg.Replace(@"http://localhost:106/Explorer/PeopleDirectory/", "/Explorer/PeopleDirectoryProxy.aspx?pdurl=$0");
var c = reg.Replace(@"http://localhost:106/Explorer/PeopleDirectory", "/Explorer/PeopleDirectoryProxy.aspx?pdurl=$0");
var d = reg.Replace(@"http://localhost:106/Explorer/PeopleDirectory.aspx", "/Explorer/PeopleDirectoryProxy.aspx?pdurl=$0");
var e = reg.Replace(@"http://localhost:106/Explorer/PeopleDirectory?param1=value1¶m2=value2", "/Explorer/PeopleDirectoryProxy.aspx?pdurl=$0");
var f = reg.Replace(@"http://localhost:106/Explorer/PeopleDirectory/?param1=value1¶m2=value2", "/Explorer/PeopleDirectoryProxy.aspx?pdurl=$0");
我所需的輸出是:
http://localhost:106/Explorer/PeopleDirectoryProxy.aspx?pdurl=assets/images/logo.png
http://localhost:106/Explorer/PeopleDirectoryProxy.aspx?pdurl=
http://localhost:106/Explorer/PeopleDirectoryProxy.aspx?pdurl=
No match (as-is)
http://localhost:106/Explorer/PeopleDirectoryProxy.aspx?pdurl=?param1=value1¶m2=value2
http://localhost:106/Explorer/PeopleDirectoryProxy.aspx?pdurl=?param1=value1¶m2=value2
電流輸出是:
http://localhost:106/Explorer/PeopleDirectoryProxy.aspx?pdurl=/Explorer/PeopleDirectory/assets/images/logo.png
http://localhost:106/Explorer/PeopleDirectoryProxy.aspx?pdurl=/Explorer/PeopleDirectory
http://localhost:106/Explorer/PeopleDirectoryProxy.aspx?pdurl=/Explorer/PeopleDirectory
No match (as-is)
http://localhost:106/Explorer/PeopleDirectoryProxy.aspx?pdurl=/Explorer/PeopleDirectory?param1=value1¶m2=value2
http://localhost:106/Explorer/PeopleDirectoryProxy.aspx?pdurl=/Explorer/PeopleDirectory/?param1=value1¶m2=value2
稍後假設結果的URL編碼。如何擺脫出現在輸出中的/ Explorer/PeopleDirectory /?
我以爲我只是捕獲/ Explorer/PeopleDirectory ....部分之後的部分,以便當我使用$ 0引用它時,它只會捕獲括號中的部分?有人可以解釋我犯錯的地方嗎?