为什么" $ 1 QUOT;在我Regex.Replace()的结果结束了?

编程入门 行业动态 更新时间:2024-10-25 18:36:02
本文介绍了为什么" $ 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=,更换字符串变成$ 110.15.15.15:8008 /代理URL = HTTP://。它包含组号码110,这肯定是不存在的参考。

When proxyStr = "10.15.15.15:8008/proxy?url=", the replacement string becomes "$110.15.15.15:8008/proxy?url=". 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()的结果结束了?

本文发布于:2023-11-04 00:46:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1556496.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:在我   结束了   QUOT   amp   Regex

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!