我正在使用 .NET 的正则表达式引擎编写以下正则表达式:
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 引用它时,它只会捕获括号中的部分?有人可以解释我在哪里出错了吗?