简体   繁体   English

奇怪的行为取代“\\”

[英]Odd behaviour replacing “\”

I have this issue in which I have a strnig with among other things the literal expression "\\\\" on several occasions and I want to replace it with "\\" , when I try to replace it with string.replace, only replcaes the first occurrence, and if I do it with regular expression it doesn't replace it at all 我有这个问题,其中有一些strnig,其中包括文字表达式"\\\\"在几个场合,我想用"\\"替换它,当我尝试用string.replace替换它时,只复制第一个发生,如果我使用正则表达式,它根本不替换它

I checked with some RegEx Testers online and supposedly my code is ok, returns what I meant to, but my code doesn't work at all 我在线查看了一些RegEx测试人员,据说我的代码没问题,返回我的意思,但我的代码根本不起作用

With string.replace 使用string.replace

example = "\\\\url.com\\place\\anotherplace\\extraplace\\";

example = example.replace("\\\\","\\");

returns example == "\\url.com\\place\\anotherplace\\extraplace\\";

With RegEx 使用RegEx

example = Regex.Replace(example,"\\\\","\\");

returns example = "\\\\url.com\\place\\anotherplace\\extraplace\\";

It is the same case if I use literals (On the Replace function parameters use (@"\\\\", @"\\") gives the same result as above). 如果我使用文字是相同的情况(在替换函数参数上使用(@"\\\\", @"\\")给出与上面相同的结果)。

Thanks! 谢谢!

EDIT: 编辑:

I think my ultimate goal was confusing so I'll update it here, what I want to do is: 我认为我的最终目标是令人困惑所以我会在这里更新它,我想要做的是:

Input: variable that holds the string: "\\\\\\\\url.com\\\\place\\\\anotherplace\\\\extraplace\\\\" 输入:包含字符串的变量: "\\\\\\\\url.com\\\\place\\\\anotherplace\\\\extraplace\\\\"

Process 处理

Output variable that holds the string "\\\\url.com\\place\\anotherplace\\extraplace\\" (so I can send it to ffmpeg and it recognizes it as a valid route) 保存字符串"\\\\url.com\\place\\anotherplace\\extraplace\\" 输出变量(因此我可以将其发送到ffmpeg并将其识别为有效路径)

change this: 改变这个:

example = "\\\\url.com\\place\\anotherplace\\extraplace\\"; 

to this 对此

example = @"\\\\url.com\\place\\anotherplace\\extraplace\\"; 

It wasn't the Regex.Replace parameters that was the problem. 问题不在于Regex.Replace参数。

You only have one occurrence of \\\\\\\\ in your string. 您的字符串中只出现一次\\\\\\\\ So it is doing exactly what you asked it to do. 所以它完全按照你的要求去做。

Without escaping (ie without adding extra /'s) 没有逃避(即没有添加额外的/)

  • What is your actual input? 你的实际投入是什么?
  • What is your desired output? 你想要的产量是多少?

您应该将其更改为以下内容

example = example.replace(@"\\", @"\");

That appears to be the expected behavior. 这似乎是预期的行为。

In the String.Replace case: Initially, example contains a string that starts with two backslashes, and contains a few single backslashes elsewhere in the string. 在String.Replace的情况下:最初,example包含一个以两个反斜杠开头的字符串,并在字符串的其他地方包含一些单个反斜杠。 You then attempted to replace all occurrences of double backslashes with a single backslash, which worked and produced a string that starts with a single backslash and contains a few single backslashes elsewhere in the string. 然后,您尝试使用单个反斜杠替换所有出现的双反斜杠,该反斜杠工作并生成一个以单个反斜杠开头的字符串,并在字符串中的其他位置包含一些单个反斜杠。

In the Regex.Replace case: The original contents of example are irrelevant in this case. 在Regex.Replace案例中:在这种情况下,示例的原始内容是无关紧要的。 Your regex pattern is a double backslash, which when interpreted as a regex pattern, means "find a single backslash". 你的正则表达式模式是一个双反斜杠,当被解释为正则表达式模式时,意味着“找到一个反斜杠”。 You then replace this pattern with a single backslash, which results in no change to the string. 然后用一个反斜杠替换此模式,这样就不会对字符串进行更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM