Three stages of a real world example.
protected virtual string ReplaceFunctions(string messageTemplate) {
string messageBody = messageTemplate;
Regex ex = new Regex(@"\$(?<name>[^\$]+)\$");
MatchCollection functions = ex.Matches(messageTemplate);
foreach(Match function in functions) {
if(function.Groups["name"].Success) {
messageBody = messageBody.Replace(string.Format("${0}$", function.Groups["name"].Value), "");
}
}
return messageBody;
}
protected virtual string ReplaceFunctions(string messageTemplate) {
string messageBody = messageTemplate;
Regex ex = new Regex(@"\$(?<name>[^\$]+)\$");
MatchCollection functions = ex.Matches(messageTemplate);
foreach(var function in (from Match f in functions where f.Groups["name"].Success select f)) {
messageBody = messageBody.Replace(string.Format("${0}$", function.Groups["name"].Value), "");
}
return messageBody;
}
protected virtual string ReplaceFunctions(string messageTemplate) {
string messageBody = messageTemplate;
Regex ex = new Regex(@"\$(?<name>[^\$]+)\$");
MatchCollection functions = ex.Matches(messageTemplate);
functions.OfType<Match>().Where(function => function.Groups["name"].Success).Select(function => messageBody.Replace(string.Format("${0}$", function.Groups["name"].Value), ""));
return messageBody;
}
No comments:
Post a Comment