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\\'

Answers


No need for regex, even.

var newStr = oldStr.Replace("\\'", "'").Replace("'", "\\'");

With regex, you can find all ' that don't have \\ before them with:

[^\\]'

Need Your Help

WPF Collections and Databinding

wpf data-binding binding collections

I am new to WPF and trying to wrap my head around WPF's framework, what it does and does not do for you.

How can write logs to a file in btrace?

java trace dtrace btrace

I have following btrace script. I would like to record entry and exit of functions in a specific class.