2013-06-26 22 views
0

我正在寫使用.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&param2=value2", "/Explorer/PeopleDirectoryProxy.aspx?pdurl=$0"); 
var f = reg.Replace(@"http://localhost:106/Explorer/PeopleDirectory/?param1=value1&param2=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&param2=value2 
http://localhost:106/Explorer/PeopleDirectoryProxy.aspx?pdurl=?param1=value1&param2=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&param2=value2 
http://localhost:106/Explorer/PeopleDirectoryProxy.aspx?pdurl=/Explorer/PeopleDirectory/?param1=value1&param2=value2 

稍後假設結果的URL編碼。如何擺脫出現在輸出中的/ Explorer/PeopleDirectory /?

我以爲我只是捕獲/ Explorer/PeopleDirectory ....部分之後的部分,以便當我使用$ 0引用它時,它只會捕獲括號中的部分?有人可以解釋我犯錯的地方嗎?

回答

0
(?:\/PeopleDirectory(?:/|))(.*) 

替換爲

/Explorer/PeopleDirectoryProxy.aspx?pdurl=$1 

只有一樣東西在.aspx將作爲一個參數,可以排除它在某些方面還是必須通過正則表達式排除?

1

我覺得你的問題是:

,你應該在你的替換字符串中使用$ 1,不$ 0

Regex reg = new Regex(@"(?i)/Explorer/PeopleDirectory/?((?!\.aspx).*)$"); 
var a = reg.Replace(@"http://localhost:106/Explorer/PeopleDirectory/assets/images/logo.png", "/Explorer/PeopleDirectoryProxy.aspx?pdurl=$1"); 
var b = reg.Replace(@"http://localhost:106/Explorer/PeopleDirectory/", "/Explorer/PeopleDirectoryProxy.aspx?pdurl=$1"); 
var c = reg.Replace(@"http://localhost:106/Explorer/PeopleDirectory", "/Explorer/PeopleDirectoryProxy.aspx?pdurl=$1"); 
var d = reg.Replace(@"http://localhost:106/Explorer/PeopleDirectory.aspx", "/Explorer/PeopleDirectoryProxy.aspx?pdurl=$1"); 
var e = reg.Replace(@"http://localhost:106/Explorer/PeopleDirectory?param1=value1&param2=value2", "/Explorer/PeopleDirectoryProxy.aspx?pdurl=$1"); 
var f = reg.Replace(@"http://localhost:106/Explorer/PeopleDirectory/?param1=value1&param2=value2", "/Explorer/PeopleDirectoryProxy.aspx?pdurl=$1"); 

這裏是替換結果:

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= 
http://localhost:106/Explorer/PeopleDirectory.aspx 
http://localhost:106/Explorer/PeopleDirectoryProxy.aspx?pdurl=?param1=value1&param2=value2 
http://localhost:106/Explorer/PeopleDirectoryProxy.aspx?pdurl=?param1=value1&param2=value2 
0

你也可以試試這個。即使您有另一個擴展名,這個匹配項也不會超過.aspx

Regex reg = new Regex(@"/Explorer/PeopleDirectory([^/\?]*[/\?]*)(.*)$"); 
var a = reg.Replace(@"http://localhost:106/Explorer/PeopleDirectory/assets/images/logo.png", "/Explorer/PeopleDirectoryProxy.aspx?pdurl=$2"); 
var b = reg.Replace(@"http://localhost:106/Explorer/PeopleDirectory/", "/Explorer/PeopleDirectoryProxy.aspx?pdurl=$2"); 
var c = reg.Replace(@"http://localhost:106/Explorer/PeopleDirectory", "/Explorer/PeopleDirectoryProxy.aspx?pdurl=$2"); 
var d = reg.Replace(@"http://localhost:106/Explorer/PeopleDirectory.aspx", "/Explorer/PeopleDirectoryProxy.aspx?pdurl=$2"); 
var e1 = reg.Replace(@"http://localhost:106/Explorer/PeopleDirectory?param1=value1&param2=value2", "/Explorer/PeopleDirectoryProxy.aspx?pdurl=$2"); 
var f = reg.Replace(@"http://localhost:106/Explorer/PeopleDirectory/?param1=value1&param2=value2", "/Explorer/PeopleDirectoryProxy.aspx?pdurl=$2");