我有这个问题,其中我有几次与文字表达式 "\\"
有冲突,我想用 "\"
替换它>,当我尝试用 string.replace 替换它时,只替换第一次出现的位置,如果我用正则表达式替换它,它根本不会替换它
我在网上与一些 RegEx 测试人员核对过,我的代码应该没问题,返回了我的意思,但我的代码根本不起作用
用string.replace
example = "\\\\url.com\\place\\anotherplace\\extraplace\\";
example = example.replace("\\\\","\\");
returns example == "\\url.com\\place\\anotherplace\\extraplace\\";
使用正则表达式
example = Regex.Replace(example,"\\\\","\\");
returns example = "\\\\url.com\\place\\anotherplace\\extraplace\\";
如果我使用文字也是一样的情况(在 Replace 函数参数上使用 (@"\\", @"\")
给出与上面相同的结果)。
谢谢!
编辑:
我认为我的最终目标令人困惑所以我会在这里更新它,我想做的是:
输入:
保存字符串的变量:"\\\\url.com\\place\\anotherplace\\extraplace\\"
过程
输出
保存字符串 "\\url.com\place\anotherplace\extraplace\"
的变量
(这样我就可以将它发送到 ffmpeg 并将其识别为有效路由)
最佳答案
改变这个:
example = "\\\\url.com\\place\\anotherplace\\extraplace\\";
对此
example = @"\\\\url.com\\place\\anotherplace\\extraplace\\";
问题不在于 Regex.Replace
参数。
关于c# - 替换 "\"的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12940313/