I have the following piece of code,
private void ReplaceVariables(DynamicForm form, DynamicEmailTemplate emailTemplate, List<DynamicFieldValue> fieldValues)
{
Regex regex = new Regex("\\?([\\w-]+)\\?", RegexOptions.IgnoreCase);
// Replace Form Display Title Variables
MatchCollection formDisplayTitleRegexMatches = regex.Matches(form.FormDisplayTitle);
if (formDisplayTitleRegexMatches.Count > 0)
{
foreach (Match formDisplayTitleRegexMatch in formDisplayTitleRegexMatches)
{
foreach (var fieldValue in fieldValues)
{
var formformDisplayTitleRegexMatchRemovedQuestionMarks = formDisplayTitleRegexMatch.Groups[1].Value;
if (formformDisplayTitleRegexMatchRemovedQuestionMarks.Equals(fieldValue.FieldCode))
{
form.FormDisplayTitle = form.FormDisplayTitle.Replace(formDisplayTitleRegexMatch.Value, fieldValue.Value);
}
}
}
}
}
IEnumerable<string> labelsInEmailTemplate = emailTemplate
.Entries
.Select(x => x.FormField.FieldDefinition.FieldLabel);
foreach (string labelInEmailTemplate in labelsInEmailTemplate)
{
MatchCollection labelRegexMatches = regex.Matches(labelInEmailTemplate);
if (labelRegexMatches.Count > 0)
{
foreach (Match labelRegexMatch in labelRegexMatches)
{
foreach (DynamicFieldValue fieldValue in fieldValues)
{
string labelRegexMatchRemovedQuestionMarks = labelRegexMatch.Groups[1].Value;
if (labelRegexMatchRemovedQuestionMarks.Equals(fieldValue.FieldCode))
{
DynamicEmailTemplateEntry emailTemplateEntryToUpdate = emailTemplate
.Entries
.Find(x => x
.FormField
.FieldDefinition
.FieldLabel
.Contains(labelRegexMatch.Value));
emailTemplateEntryToUpdate
.FormField
.FieldDefinition
.FieldLabel = emailTemplateEntryToUpdate
.FormField
.FieldDefinition
.FieldLabel
.Replace(labelRegexMatch.Value, WebUtility.HtmlEncode(fieldValue.Value));
}
}
}
}
}
Sample title variable can be - ?MY_Title? and ?MyName?
I have 2 foreach loops, instead, is it possible to use MatchEvaluator and a delegate passing to Regex.Replace?
Made another update, is it possible to use Replace when I have 2 foreach loops, as I cannot update the record through foreach loop.