RegEx无法匹配数据

时间:2014-03-06 12:33:52

标签: c# regex

我正在使用RegEx来查找和替换数据。 假设我的代码是

Dictionary<string, string> data=new Dictionary<string, string>();
data.Add("JID","421587");

string template ="Your unique job number is {JID} .Your part will be collected by us"
string result = Regex.Replace(template, @"{([^{}]+)}", delegate(Match match)
{
     string key = match.Groups[1].Value;
     return data[key];
});
_body = result;
return this;

实际上我正在尝试找出这个字符{JID},并希望将{JID}替换为存储在字典中的值,但我想我在这里使用的模式@"{([^{}]+)}是不对的查找数据并将其替换为{JID}

所以请帮助我如何从字符串中找到{JID}并替换。感谢

修改

实际上我试图替换的文本是巨大的html文本,这可能就是问题发生的原因。

所以现在我以这种方式管理整个事情。

public MailTemplate ParseTemplate(Dictionary<string, string> data)
        {
            string template = GetBody();
            foreach(KeyValuePair<string, string> item in data)
            {
                if (item.Key.ToUpper() != "ATTACHEDFILEPATH")
                {
                    template = template.Replace("{" + item.Key + "}", item.Value);
                }
            }
            _body = template;
            return this;
        }

0 个答案:

没有答案