Ram
Ram

Reputation: 51

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

Upvotes: 5

Views: 2687

Answers (4)

Kobi
Kobi

Reputation: 137997

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

Upvotes: 3

svick
svick

Reputation: 244757

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' 

Upvotes: 5

Igoris
Igoris

Reputation: 1650

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.

Upvotes: 0

hungryMind
hungryMind

Reputation: 6999

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

Upvotes: -1

Related Questions