简体   繁体   中英

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

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, ie 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.

NB 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, eg:

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

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

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.

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.

尝试$1\\r\\n - 它在VS 2013中适用于我。假设您需要一个新行,而不是专门\\n

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM