0

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.

1 Answer 1

2

Yes, you can try rewriting you code into Regex.Replace, something like this:

Regex regex = new Regex(@"\?[\w-]+\?", RegexOptions.IgnoreCase);

form.FormDisplayTitle = regex.Replace(form.FormDisplayTitle, match => {
  // get rid of leading and trailing ?
  var name = match.Value.Trim('?');

  // check if match corresponds to any FieldCode
  foreach (var fieldValue in fieldValues) 
    if (name.Equals(fieldValue.FieldCode))
      return fieldValue.Value; // Code found, Value returned
  
  // Unknown FieldCode, do nothing (return match intact)
  return match.Value; 
});

Here for each match lambda function is called where we should return the substitution: either fieldValue.Value or match itself - match.Value - if value has not been found.

Update: The second fragment can be rewritten as follow:

// For each entry in emailTemplate.Entries
foreach (var DynamicEmailTemplateEntry entry in emailTemplate.Entries) {
  // given lable teemplate...
  string labelTemplate = entry
    .FormField
    .FieldDefinition
    .FieldLabel;

  // ... we compute actual label
  string updatedLabel = regex.Replace(labelTemplate, match => {
    var name => match.Value.Trim('?');
  
    // either FieldValue or match as it is
    return fieldValue
      .Where(fv => fv.FieldCode == name)
      .Select(fv => fv.Value)
      .FirstOrDefault(match.Value);
  });

  // which we assign to template 
  entry
    .FormField
    .FieldDefinition
    .FieldLabel = updatedLabel; 
}
Sign up to request clarification or add additional context in comments.

2 Comments

Solution works great, thanks! I updated my question, with another method there are 2 foreach loops, and I cannot update the record in foreach as it is read-only. Is it possible to use Replace there?
@tRuEsAtM: The very same princile: for each entry within Entries we get template, replace placeholders with a help of regular expresssions; then we assign label back.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.