private void PerformVariableReplacement(StringBuilder builder, string text) { var start = 0; var length = text.Length; while (start < length) { var match = VariableRegex.Match(text, start); if (!match.Success) { builder.Append(text, start, length - start); return; } builder.Append(text, start, match.Index); builder.Append(GetVariableReplacement(match)); start = match.Index + match.Length; } }
/// <summary> /// Replaces the variable names with repective values in the supplied string. /// </summary> /// <param name="input">The string which to have the evaluation applied.</param> /// <param name="hostVariables">The collection of variables to be queried.</param> /// <returns>String</returns> public static string EvaluateVariable(string input, Dictionary <string, string> hostVariables) { if (hostVariables != null && !string.IsNullOrWhiteSpace(input)) { var match = VariableRegex.Match(input); if (match.Success) { do { var key = match.Groups[1].Value; if (hostVariables.ContainsKey(key)) { input = input.Replace(match.Value, hostVariables[key]); } } while ((match = match.NextMatch()).Success); } } return(input); }
/// <summary> /// /// </summary> /// <param name="exogenous" /// /// ></param> /// <param name="sets"> /// /// </param> /// <returns> /// /// </returns> private static IEnumerable <VariableDefinition> SetExogenousVariables(string exogenous, IEnumerable <KeyValuePair <string, IImmutableList <string> > > sets) { sets = sets as KeyValuePair <string, IImmutableList <string> >[] ?? sets.ToArray(); foreach (Match match in VariableRegex.Matches(exogenous)) { VariableDefinition definition = new VariableDefinition( match.Groups["variable"].Value, true, match.Groups["indexes"] .Captures .Cast <Capture>() .Where(y => y.Length > 0) .Select(y => y.Value)); if (definition.Indexes.All(x => x.StartsWith("\""))) { yield return (new VariableDefinition( definition.Name, definition.IsExogenous, definition.Indexes.Select(x => x.Replace("\"", null).Replace(",", null)))); continue; } foreach (KeyValuePair <string, IImmutableList <string> > item in sets.Where(x => definition.Indexes.Contains(x.Key))) { foreach (string entry in item.Value) { yield return(new VariableDefinition(definition.Name, true, definition.Indexes.Replace(item.Key, entry).Select(x => x.Replace("\"", null).Replace(",", null)))); } } } }
private static bool HasVariableReplacement(string text) { return(VariableRegex.IsMatch(text)); }
public static List <string> GetVariables(string documentPath) { return(string.IsNullOrWhiteSpace(documentPath) ? new List <string>() : VariableRegex.Matches(documentPath).OfType <Match>().Select(x => VariableNameRegex.Match(x.Value).Value.Trim()).ToList()); }
public static bool ContainsVariable(string text) { return(!string.IsNullOrWhiteSpace(text) && VariableRegex.IsMatch(text)); }