Hello AlexS,
Unless you want to replace them all at the same time, using Regex is a very
expensive operation to do single string replacements.
mystring.Replace("what", "replacement")
will get you there.
If you want to replace multiple characters at once, then it gets more exiting.
You can use a regex to specify all of the characters that need to have somthign
prefixed by grouping them into a characterset.
The syntax for a characterset is [characters].
So if you want to replace both ' and " you can put them in a set like this:
["'].
Now for the replacement. The replacement patterns are a little different
than you're used to, because it is possible to include the text you originally
found. This a a very useful feature.
A replacement pattern would look like this:
\$0
\ for the prepended backslash you want to add. And $0 for the string that
we originally found (so in our case either a ' or a ").
Now putting this together yields:
string original = "...";
string replaced = Regex.Replace(original, @"[""'], @"\$0");
and you're all there.
Notice how I used @"..." to make the regex more readable. In a verbatim string
(that's what the @"..." is called) you only need to escape the " by doubling
it up.
This should get you where you want to go...
Kind regards,
Jesse Houwing