How to ignore text inside a quoted string in .NET. I have following string
This is test, 'this is test inside quote'
Say I'm searching for test
and replacing it should only replace test
not present inside the quote.
This is, 'this is test inside quote'.
I m using this to match text inside the quoted text.
(["']).*?\1
I would use Regex.Replace()
. The regex would match a non-quoted string followed by a quoted string and the match evaluator would replace test
in the non-quoted part. Something like this:
Regex.Replace("This is test, 'this is test inside quote' test",
@"(.*?)((?<quote>[""']).*?\k<quote>|$)",
m => m.Groups[1].Value.Replace("test", "") + m.Groups[2].Value)
Group 1 is the non-quoted part, group 2 is the quoted part (or the end of the string). The result of the above is:
This is , 'this is test inside quote'
You can use the following pattern to skip over quoted strings:
s = Regex.Replace(s, @"test|(([""']).*?\2)", "$1");
On each character of your string the pattern can match the string "test"
, match and capture a quoted string, or fail. If it does capture the $1
group it will be preserved after replacement, otherwise the matched string will be removed.
Working example: http://ideone.com/jZdMy
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