RegEx ignore text inside quoted strings in .net
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
I would extract these quoted substrings into a List (of course if you have more than 1 quotation),create placeholder for later (%1,%2, etc.), execute the regexp, and replace placeholders with list items.
I can see one double quote and one single quote in regEx. Ensure both are single quote. Perhaps you need to escape the single quotes too. ([\'\']).*?\1
精彩评论