C#替换字符串,除非前面有另一个字符串

flo*_*low 4 c# regex string replace

我想,以取代所有ocurrence "通过\"在一个字符串,除非这"前面有一个\ 用于为例字符串hello "World\"将成为hello \"World\"

是否可以不使用正则表达式?但如果我必须使用正则表达式,我可以使用什么样的?

谢谢你的帮助,问候,

p.s*_*w.g 6

你可以使用lookbehind:

var output = Regex.Replace(input, @"(?<!\\)""", @"\""")
Run Code Online (Sandbox Code Playgroud)

或者你可以让前面的字符可选,例如:

var output = Regex.Replace(input, @"\\?""", @"\""")
Run Code Online (Sandbox Code Playgroud)

这是有效的,因为"被替换为\"(这是你想要的),并被\"替换为\",所以没有改变.