简体   繁体   English

如何在MSVS 2012+编辑器(和.NET?)中使用正则表达式替换插入'\\'+'n'

[英]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? 在Visual Studio 2013的编辑器中,我理解它与2012非常相似, 据称使用.NET正则表达式,我无法获得替换字符串以插入反斜杠和' n ' - 甚至可能吗?

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. 我希望在C程序的某些(但不是全部)行的第一个“ "之后插入' \\n ',即使字符串文字以新行开头。显然,要查找的表达式是' ^([^"]*)" ',替换是......什么?' $1" '+ magic +' n ',其中魔术就是插入反斜杠。 I am not trying to write .NET code to do this, but to do it interactively in the editor. 我不是想编写.NET代码来执行此操作,而是在编辑器中以交互方式执行此操作。

NB This question does not apply to MSVS 2010 , which has an older RE syntax. 注意这个问题并不适用于2010 MSVS,它有一个较旧的RE语法。

Officially … 正式 …

None of the Substitutions in the .NET reference is appropriate. .NET引用中的替换都不合适。
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! Regular-expressions.info说' 你不能也不必逃避反斜杠,感叹号或除了美元之外的任何其他字符,因为它们在.NET,JavaScript,VBScript和PCRE2替换字符串中没有特殊含义 ' - 但是MSVS RE Replace似乎将替换中的' \\n '视为新行!
RegexHero (meant for .NET) says that ' ^([^"]*)" '→' $1"\\n ' transforms ' asdf"qwer '→' asdf"\\nqwer ', which agrees with Regular-expressions.info . RegexHero (用于.NET)表示' ^([^"]*)" '→' $1"\\n '转换' asdf"qwer '→' asdf"\\nqwer ',这与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. 我的印象 - 恐惧 - 是MSVS IDE在将它传递给RE引擎之前在编辑对话框中转换了一些C风格的转义,例如' \\n ',' \\r '和' \\t ',但是留下了其他的反斜杠不变。 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; 鉴于记录的RE引擎行为,这是对我有意义的唯一解释; the conclusion is that you cannot insert a backslash that is interpreted as part of a C-style escape. 结论是您不能插入被解释为C样式转义的一部分的反斜杠。

Experiments 实验

I have tried various things for magic : 我为魔术尝试了各种各样的东西:

  • These all yield themselves, even before ' n ': 即使在' n '之前,这些都会自我产生:
    • ' \\\\ ' ' \\\\ '
    • ' \\134 ' ' \\134 '
    • ' \\x5cn ' ' \\x5cn '
  • ' \\ ' before ' n ' yields a new line \\ ”前“ n ”产生一个新的线
  • ' $\\ ' before ' n ' yields ' $ ' + a new line $\\ ”之前“ n ”收益率“ $ ” +新行

Workaround 解决方法

I suspect I have to do a two step job, eg: 我怀疑我必须做两步工作,例如:

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

… that works, but is clumsier and more error-prone than a one-step solution. ......有效,但比一步解决方案更笨拙,更容易出错。

I wish to insert '\\n' after the first '"' 我希望在第一个'''之后插入'\\ n'

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++. 我会使用NotePad ++作为正则表达式^\\x22 (\\ x22是引用的转义,我在处理正则表达式模式时的习惯,而不是字面上逃避C#编译器的引用例如"" = \\x22 )和替换\\\\n工作并用Notepad ++中的文字\\n替换行的第一个引号。


Note see other anomalies reported when dealing with special characters: 注意查看处理特殊字符时报告的其他异常:

Find and Replace dialog regex replacement can't escape a "$" symbol | 查找和替换对话框正则表达式替换无法转义“$”符号| Microsoft Connect Microsoft Connect


In the older versions of Visual Studios with Macro capabilities, this could be accomplished. 在具有宏功能的Visual Studio的旧版本中,这可以实现。

Your regular expression appear to work. 您的正则表达式似乎有效。 The following code will display true: 以下代码将显示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. 也许你需要一个"\\r\\n"作为@ stephen.vakil建议或其他东西导致你的代码出现问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .NET RegEx有条件替换 - .NET RegEx conditional replacement 如何使用 Visual Studio 2012 在 XP 上定位 .NET 4.0.3? 应用程序无法运行,“不是有效的 Win32 应用程序” - How do I target .NET 4.0.3 on XP using Visual Studio 2012? Application fails to run with “not a valid Win32 application” 使用XSLT,如何在.NET项目中使用变量将属性与正则表达式匹配? - Using XSLT, how do I match an attribute with regex using a variable in a .NET project? 如何在.NET Regex Replace中转义替换/替换部分 - How to escape the substitution/replacement part in .NET Regex Replace 如何使用Regex类从.NET字符串中删除所有字母数字字符? - How do I remove all alphanumeric characters from a .NET string using the Regex class? 如何使用.Net的RegEx从字符串中提取所有{}标记? - How do I extract all {} tokens from a string using .Net's RegEx? 如何使用 c# (.net) 播放“Windows 硬件插入”和“Windows 硬件删除”声音? - How do i play the "Windows Hardware Insert" and "Windows Hardware Remove" sounds using c# (.net)? 如何在正则表达式中命名结果组? (。网) - How do I name a result group in a Regex? (.Net) 如何用.net中的新String替换第n个正则表达式组? - How do I replace the nth regex group with a new String in .net? 如何使Regex操作超时以防止在.NET 4.5中挂起? - How do I timeout Regex operations to prevent hanging in .NET 4.5?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM