0

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

4

3 回答 3

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
于 2013-06-26T07:33:56.687 回答
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");
于 2013-06-26T07:42:36.607 回答
0
(?:\/PeopleDirectory(?:/|))(.*)

用替换

/Explorer/PeopleDirectoryProxy.aspx?pdurl=$1

.aspx 只有一件事将用作参数,您可以以某种方式排除它,还是我必须通过正则表达式排除?

于 2013-06-26T07:32:38.580 回答