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

あなたの問題は次のとおりだと思います:

$0ではなく、 $1を置換文字列に使用する必要があります。

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
(?:\/PeopleDirectory(?:/|))(.*)

置き換えて

/Explorer/PeopleDirectoryProxy.aspx?pdurl=$1

.aspxがパラメータとして使用されるのは1つだけです。何らかの方法で除外できますか、それとも正規表現で除外する必要がありますか?

于 2013-06-26T07:32:38.580 に答える
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 に答える