Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx ignore text inside quoted strings in .net

Tags:

.net

regex

quotes

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
like image 743
Ram Avatar asked Jul 12 '11 21:07

Ram


2 Answers

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' 
like image 57
svick Avatar answered Sep 21 '22 21:09

svick


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

like image 30
Kobi Avatar answered Sep 25 '22 21:09

Kobi