正则表达式 - 无法识别的转义序列 [英] regex - unrecognized escape sequence
本文介绍了正则表达式 - 无法识别的转义序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
来自: https://stackoverflow.com/a/959982/101055
我试图使用:
using System.Text.RegularExpressions;
Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("goodName1", "asdf");
parameters.Add("goodName2", "qwerty");
string text = "this is my {goodName1} template {goodName2} string";
text = Regex.Replace(text, "\{(.+?)\}", m => parameters[m.Groups[1].Value]);
我在 \ {(。+?)\\ \\}
,在 {
和}
。
错误>无法识别的转义序列
ERROR > Unrecognized escape sequence
这里有什么问题? >
What is wrong here?
推荐答案
尝试:
text = Regex.Replace(text, @"\{(.+?)\}", m => parameters[m.Groups[1].Value]);
更多详细信息。
@符号定义了一个Verbatim字符串文字。这基本上说是告诉编译器,引号中的字符串没有被转义。 http://msdn.microsoft.com/en-我们/ library / aa691090(v = vs.71).aspx
The @ sign defines a Verbatim String Literal. This basically says tells the compiler that the string in quotes is not escaped. http://msdn.microsoft.com/en-us/library/aa691090(v=vs.71).aspx
可选的是,您可以简单地在斜杠上加倍。例如:
Optionally, you could have simply doubled up on the back slashes. e.g.:
text = Regex.Replace(text, "\\{(.+?)\\}", m => parameters[m.Groups[1].Value]);
但是,IMHO,@符号更易读。
But, IMHO, the @ sign is much more readable.
这篇关于正则表达式 - 无法识别的转义序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文