Why is “$1” ending up in my Regex.Replace() result?

后端 未结 4 707
北恋
北恋 2021-01-17 22:10

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

bodystring = Regex.Replace(bodystring, \"(src=\'/+)\", \"$1\" + proxySt         


        
相关标签:
4条回答
  • 2021-01-17 22:30

    In the second version, I guess proxyStr appears twice because you're inserting it once more. Try

    string s2 = Regex.Replace(s, "((?<=src='/+))", proxyStr);
    
    0 讨论(0)
  • 2021-01-17 22:39

    What you are doing can't be done. .NET has trouble when interpolating variable like this. Your problem is that your Proxy string starts with a number : proxyStr = "10.15.15.15:8008/proxy?url=http://"

    When you combine this with your $1, the regex thing it has to look for backreference $110 which doesn't exist.

    See what I mean here.

    You can remedy this by matching something else, or by matching and constructing the replacement string manually etc. Use what suits you best.

    0 讨论(0)
  • 2021-01-17 22:45

    Based on dasblinkenlights answer (already +1) the solution is this:

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

    This ensures that the group 1 is used and not a new group number is build.

    0 讨论(0)
  • 2021-01-17 22:48

    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);
    

    Edit:

    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);
    
    0 讨论(0)
提交回复
热议问题