Example #1
0
        /// <summary>
        /// Utility method for evaluating the Regex Matches in ReplaceVariations()
        /// </summary>
        private static async Task <string> EvaluateVariation(Match match, string caseSensitiveOldValue, string caseSensitiveNewValue, StringConflictHandler conflictHandler)
        {
            if (match.Value == caseSensitiveOldValue.ToUpper())
            {
                return(caseSensitiveNewValue.ToUpper());
            }

            if (match.Value == caseSensitiveOldValue.FirstCharacterToLower())
            {
                if (match.Value == caseSensitiveOldValue.ToLower())
                {
                    // resolve conflict (e.g. should dissolve => foofilter or dissolve => fooFilter)
                    return(await conflictHandler(match.Value, caseSensitiveNewValue.ToLower(), caseSensitiveNewValue.FirstCharacterToLower()));
                }
                else
                {
                    return(caseSensitiveNewValue.FirstCharacterToLower());
                }
            }

            if (match.Value == caseSensitiveOldValue.ToLower())
            {
                return(caseSensitiveOldValue.ToLower());
            }

            return(caseSensitiveNewValue);
        }
Example #2
0
 /// <summary>
 /// Using case-insensitive regex matching, determines the variation of the match, and replaces it with the appropriate variation of the desired result
 /// e.g. ReplaceVariations("Hello WORLD", "hello", "goodbye") == "Goodbye WORLD", and ReplaceVariations("Hello WORLD", "world", "friend") == "Hello FRIEND"
 /// </summary>
 /// <param name="string">unadultered string to replace</param>
 /// <param name="caseSensitiveOldValue">the old value to replace, in desired case, e.g. "OldFilter"</param>
 /// <param name="caseSensitiveNewValue">the new value, in desired case, e.g. "NewFilter"</param>
 /// <param name="conflictHandler">an async callback for optionally getting user feedback from the main thread</param>
 /// <returns>a string with all variation replacements applied</returns>
 public static string ReplaceVariations(this string @string, string caseSensitiveOldValue, string caseSensitiveNewValue, StringConflictHandler conflictHandler)
 {
     return(Regex.Replace(
                input: @string,
                pattern: caseSensitiveOldValue,
                evaluator: (Match match) =>
     {
         var task = EvaluateVariation(match, caseSensitiveOldValue, caseSensitiveNewValue, conflictHandler);
         task.Wait();
         return task.Result;
     },
                options: RegexOptions.IgnoreCase));
 }