C#替换字符串时,前面还有一个除 [英] C# replace string except when preceded by another
本文介绍了C#替换字符串时,前面还有一个除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要替换的所有ocurrence 按
\
在一个字符串,除非这个是由
\
为为例字符串开头你好World\
将成为你好\World\
I want to replace all ocurrence of "
by \"
in a string except if this "
is preceded by a \
for exemple the string hello "World\"
will become hello \"World\"
是否有可能不使用正则表达式?
但是如果我必须使用正则表达式,什么样的都有我使用?
Is it possible without using regex ? But if I have to use regex, what kind have I to use ?
感谢您的帮助,
的问候,
Thanks for help, regards,
推荐答案
您可以使用后向:
var output = Regex.Replace(input, @"(?<!\\)""", @"\""")
或者你可以只是使前述人物可选,例如:
Or you could just make the preceeding character optional, for example:
var output = Regex.Replace(input, @"\\?""", @"\""")
这工作,因为替换
\
(这是你想要的)和 \
替换 \
,所以没有任何变化。
This works because "
is replaced with \"
(which is what you wanted), and \"
is replaced with \"
, so no change.
这篇关于C#替换字符串时,前面还有一个除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文