Replace a pattern in string only if a certain condition is satisfied - Regex
how can we replace ' with \\' in a string. (this can be done using Regex.IsMatch(), Regex.Matches(), Regex.Replace() However, it should be done only if ' doesn't have \ or \\ before already. (this is where I am stuck)
That means find all ' which do not have \ or \\ before it and then add the same, i.e. ' replace with \\'
Example string: 'abcd\'efg'hijkl'mno\\'pqrs'
Resulting string: \\'abcd\\'efg\\'hijkl\\'mno\\'pqrs\\'
No need for regex, even.
var newStr = oldStr.Replace("\\'", "'").Replace("'", "\\'");
With regex, you can find all ' that don't have \\ before them with: