RegEx ignore text inside quoted strings in .net

时光总嘲笑我的痴心妄想 提交于 2019-12-23 10:17:40

问题


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

回答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' 



回答2:


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




回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/6671196/regex-ignore-text-inside-quoted-strings-in-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!