URL使用捕获组重写正则表达式

时间:2013-06-26 06:38:08

标签: .net regex

我正在使用.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&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引用它时,它只会捕获括号中的部分?有人可以解释我错误的地方吗?

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

with replace

/Explorer/PeopleDirectoryProxy.aspx?pdurl=$1

只有一件事.aspx将用作参数,你可以用某种方式将其排除,或者我必须通过正则表达式排除?

答案 2 :(得分: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");