How do I insert ‘\’+‘n’ using regex replacement in MSVS 2012+ editor (and .NET?)

烂漫一生 提交于 2020-01-03 20:30:28

问题


In the editor of Visual Studio 2013, which I understand is very similar to 2012 and allegedly uses .NET regexes, I cannot get the replacement string to insert a backslash and an ‘n’ — is that even possible?

I wish to insert ‘\n’ after the first ‘"’ on some (but not all) lines of a C programme, i.e. make the string literals start with a new line. Obviously the expression to find is ‘^([^"]*)"’, and the replacement is … what? ‘$1"’ + magic + ‘n’, where magic is whatever inserts a backslash. I am not trying to write .NET code to do this, but to do it interactively in the editor.

N.B. This question does not apply to MSVS 2010, which has an older RE syntax.

Officially …

None of the Substitutions in the .NET reference is appropriate.
Regular-expressions.info says ‘You can't and needn't escape the backslash, exclamation point, or any other character except dollar, because they have no special meaning in .NET, JavaScript, VBScript, and PCRE2 replacement strings’ — but MSVS RE Replace appears to treat ‘\n’ in the replacement as a new line!
RegexHero (meant for .NET) says that ‘^([^"]*)"’→‘$1"\n’ transforms ‘asdf"qwer’→‘asdf"\nqwer’, which agrees with Regular-expressions.info.

Conclusion

My impression –and fear– is that the MSVS IDE itself transforms some C-style escapes such as ‘\n’, ‘\r’ and ‘\t’ in the edit dialogue before passing it to the RE engine, but leaves other backslashes unchanged. In particular, it does not provide a form such as ‘\\’ (or ‘$\’), as would be required to make inserting backslash possible. Given the documented RE engine behaviour, this is the only interpretation that makes sense to me; the conclusion is that you cannot insert a backslash that is interpreted as part of a C-style escape.

Experiments

I have tried various things for magic:

  • These all yield themselves, even before ‘n’:
    • \\
    • \134
    • \x5cn
  • \’ before ‘n’ yields a new line
  • $\’ before ‘n’ yields ‘$’ + a new line

Workaround

I suspect I have to do a two step job, e.g.:

  • ^([^"]*)"’→‘$1"\\n
  • ^([^"]*"\\)\\’→‘$1

… that works, but is clumsier and more error-prone than a one-step solution.


回答1:


I wish to insert ‘\n’ after the first ‘"’

If one does not use regex but a standard text search and replace, it can be done. But one will need to do it by hand to skip the quote you don't want.

I would use NotePad++ for the regex ^\x22 (\x22 is the escape for quote, my habit when dealing with regex patterns and not having to literally escape the quote for the C# compiler such as "" = \x22) and replacement \\n works and replaces the first quote of a line with a literal \n in Notepad++.


Note see other anomalies reported when dealing with special characters:

Find and Replace dialog regex replacement can't escape a "$" symbol | Microsoft Connect


In the older versions of Visual Studios with Macro capabilities, this could be accomplished.




回答2:


Your regular expression appear to work. The following code will display true:

var test = "asdf\"qwer";
var replaced = Regex.Replace(test, "^([^\"]*)", "$1\n");
Console.WriteLine(replaced[4] == '\n');

Perhaps you need a "\r\n" as @stephen.vakil suggests or something else is causing a problem in your code.




回答3:


Try $1\r\n - it works for me in VS 2013. That's assuming that you want a new line and not specifically \n



来源:https://stackoverflow.com/questions/32260224/how-do-i-insert-n-using-regex-replacement-in-msvs-2012-editor-and-net

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