所以我有一个很长的字符串,我有“保留字”,我需要从数据库中取代它们的值。c#正则表达式替换动态匹配的问题?
eg.
string text = "You're salary for the month of ((month)) is ((salary))
现在我所做的就是满足每一个保留字,然后通过我的数据集搜索,然后替换那些与他们的价值
Regex ex = new Regex(@"(?<=\(\().*?(?=\)\))");
foreach(Match match in ex.Matches(body)){
string valuefromset = values.FirstOrDefault(val => val.Variable == match.Value).Value;
var pattern = @"(("+match.Value+"))";
body = Regex.Replace(body, pattern, valuefromset, RegexOptions.IgnoreCase);
}
}
现在发生的事情是这样的
text = "You're salary for the month of ((April)) is (($10000))";
我不确定为什么模式只会得到单词而不是标签。我应该使用另一个正则表达式,但具有特定的值吗?具有特定的保留字在这种模式中是重要的,这就是为什么我使用它的原因,我不确定我在做什么。
任何帮助表示赞赏。谢谢!!!
没有什么直接的关系,但你应该使用的“你”,而不是“你”。 – Shimrod 2012-04-25 13:16:50
未知令牌(保留字)是否存在于字符串中?有很多潜在的令牌吗?看起来好像使用StringBuilder的.Replace方法会更高效,更简单。 – MyItchyChin 2012-04-25 13:38:09