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,
You could use a lookbehind:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With