为什么" $ 1 QUOT;在我Regex.Replace()的结果结束了? [英] Why is "$1" ending up in my Regex.Replace() result?

查看:355
本文介绍了为什么" $ 1 QUOT;在我Regex.Replace()的结果结束了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个正则表达式来重写URL指向一个代理服务器。

I am trying to write a regular expression to rewrite URLs to point to a proxy server.

bodystring = Regex.Replace(bodystring, "(src='/+)", "$1" + proxyStr);

这表达的想法很简单,基本上找到src =/或实例 SRC =//和插入在这一点上一个代理URL。这在一般的,但偶尔我已经发现一些文字$ 1将在结果字符串结束。

The idea of this expression is pretty simple, basically find instances of "src='/" or "src='//" and insert a PROXY url at that point. This works in general but occasionally I have found cases where a literal "$1" will end up in the result string.

这是没有意义的我,因为如果没有比赛,那么为什么它在所有替换什么?

This makes no sense to me because if there was no match, then why would it replace anything at all?

可惜我不能在它只有非常大的绳子,会发生至今对此进行一个简单的例子,但我。倒是想从概念上知道什么可以做这样的事情发生。

Unfortunately I can't give a simple example of this at it only happens with very large strings so far, but I'd like to know conceptually what could make this sort of thing happen.

顺便说一句,我试图用一个积极的后向如下重写这个表达式:

As an aside, I tried rewriting this expression using a positive lookbehind as follows:

bodystring = Regex.Replace(bodystring, "(?<=src='/+)", proxyStr);



但如果输入字符串包含SRC ='这两次结束了与proxyStr输出// 。这也没有多大意义,因为我认为SRC =必须是存在于输入为了得到proxyStr在输出端两次提高一倍。

But this ends up with proxyStr TWICE in the output if the input string contains "src='//". This also doesn't make much sense to me because I thought that "src=" would have to be present in the input twice in order to get proxyStr to end up twice in the output.

推荐答案

proxyStr =10.15.15.15:8008/proxy?url=http://,更换字符串变成$ 110.15.15.15:8008 /代理URL = HTTP://。它包含组号码110,这肯定是不存在的参考。

When proxyStr = "10.15.15.15:8008/proxy?url=http://", the replacement string becomes "$110.15.15.15:8008/proxy?url=http://". It contains a reference to group number 110, which certainly does not exist.

您需要确保您的代理字符串不以数字开始。

You need to make sure that your proxy string does not start in a digit. In your case you can do it by not capturing the last slash, and changing the replacement string to "$1/"+proxyStr, like this:

bodystring = Regex.Replace(bodystring, "(src='/*)/", "$1/" + proxyStr);



编辑:

Rawling的指出是.NET的正则表达式库解决了这个问题:你可以附上 1 在大括号,以避免假走样,是这样的:

Rawling pointed out that .NET's regexp library addresses this issue: you can enclose 1 in curly braces to avoid false aliasing, like this:

bodystring = Regex.Replace(bodystring, "(src='/+)", "${1}" + proxyStr);

这篇关于为什么&QUOT; $ 1 QUOT;在我Regex.Replace()的结果结束了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆