RegEx ignores text inside quoted strings in .net

How to ignore text inside quoted string in .NET. I have a line

This is test, 'this is test inside quote'

Let's say that I am looking for testand replacing it, instead of replacing testit is not present inside the quote.

This is, 'this is test inside quote'. 

I use this to match text inside quoted text.

(["']).*?\1
+5
source share
4 answers

I would use Regex.Replace(). The regular expression will match the unquoted string, followed by the quoted string, and the matching evaluator will replace testin the non-command part. Something like that:

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-command part, group 2 is the quoted part (or end of line). The result of the above is:

This is , 'this is test inside quote' 
+5

:

s = Regex.Replace(s, @"test|(([""']).*?\2)", "$1");

"test", . $1, , .

: http://ideone.com/jZdMy

+3

(, 1 ), (% 1,% 2 ..), .

0

regEx. , . , . ([\'\']).*?\1

-1

All Articles