C# 替换C中包含括号的字符串#
我试图用给定的变量替换C#email脚本中包含括号的字符串。函数中的代码片段如下所示:C# 替换C中包含括号的字符串#,c#,regex,C#,Regex,我试图用给定的变量替换C#email脚本中包含括号的字符串。函数中的代码片段如下所示: MailDefinition md = new MailDefinition(); md.From = "testmail@test.com"; string addressee = salutation; ListDictionary replacements = new ListDictionary(); replacements.Add("\n", "<br />"); repl
MailDefinition md = new MailDefinition();
md.From = "testmail@test.com";
string addressee = salutation;
ListDictionary replacements = new ListDictionary();
replacements.Add("\n", "<br />");
replacements.Add("(Name of Employee)", addressee)
string body = EmployeeQuery.getEmailMessageByCategory(category, subcategory);
MailMessage msg = md.CreateMailMessage("admin@test.com", replacements, body, new System.Web.UI.Control());
“雇员姓名”将替换为收件人变量的值。但是,它不能替换括号
如何替换括号,以便只添加收件人的值?TIA使用以下方法:
replacements.Add("\\(Name of Employee\\)", addressee)
// \ acts as escape charecter, supressing the special meaning of next charecter
// (XYZ) matches XYZ as a group
// \\(XYZ\\) matches (XYZ)
你需要加上“\\(”和“\\)”。
@denims的答案可能部分正确,但c#本身解释字符串中的\。您可能需要\由正在调用的方法进行解释
也就是说,方法文档没有说明为什么会发生这种情况。很抱歉,它产生了一个错误:错误1无法识别的转义序列使用逐字字符串。“\”也是C#中的转义字符。你可以使用
@“\(..”
,或者像“\\(姓名…\\)”
那样手动退出。很抱歉这个错误。在C#中不知道这个事实。没关系@Denim。你的答案非常感谢。是的,它做到了。实际上@AndrewBacker在评论Denim的答案时首先正确地得到了它
replacements.Add("\\(Name of Employee\\)", addressee)
// \ acts as escape charecter, supressing the special meaning of next charecter
// (XYZ) matches XYZ as a group
// \\(XYZ\\) matches (XYZ)