/// <summary> /// Initialize a new instance of HtmlKeywordsReplacer class. /// </summary> /// <param name="providers">Keywords providers.</param> /// <exception cref="System.ArgumentException"><paramref name="providers"/> is null or empty.</exception> public HtmlKeywordsReplacer(params IHtmlKeywordReplacementProvider[] providers) { if (providers == null || providers.Length == 0) { throw new ArgumentException("Keywords providers is null or empty.", "providers"); } StringBuilder pattern = new StringBuilder(); foreach (IHtmlKeywordReplacementProvider provider in providers) { if (provider == null) { continue; } foreach (HtmlKeywordReplacementInfo info in provider.Keywords) { pattern.AppendFormat("|{0}", RegexUtility.Encode(info.Keyword)); this.m_keywords[info.Keyword.ToLower()] = info; } this.m_providers.Add(provider); } if (pattern.Length > 0) { this.m_regex = new Regex(pattern.Remove(0, 1).ToString(), RegexOptions.IgnoreCase | RegexOptions.Compiled); } }
public OperationExpressionCalculator(IEnumerable <IOperationRule> rules) { if (rules == null || !rules.Any()) { throw new ArgumentException("rules is null ro empty", "rules"); } StringBuilder pattern = new StringBuilder(string.Format("(?:{0}[^{0}]*{0})", STRING_DELIMITER)); foreach (string item in rules.Select((item) => item.OperandPattern).Where((item) => item != null).OrderByDescending((item) => item.Length).Distinct(StringComparer.OrdinalIgnoreCase)) { pattern.AppendFormat("|(?:{0})", item); } foreach (string item in rules.Select((item) => item.OperatorSymbol).OrderByDescending((item) => item.Length).Distinct(StringComparer.OrdinalIgnoreCase)) { pattern.AppendFormat("|(?:{0})", RegexUtility.Encode(item)); } this.m_whitespaceRegex = new Regex("\\s+", RegexOptions.Compiled); this.m_expressionRegex = new Regex(pattern.ToString(), RegexOptions.Compiled | RegexOptions.IgnoreCase); this.m_wellFormedRegex = new Regex(pattern.AppendFormat("|\\{0}|\\{1}", LEFT_BRACKET, RIGHT_BRACKET).ToString(), RegexOptions.Compiled | RegexOptions.IgnoreCase); this.m_bracketRegex = new Regex(string.Format( "(?>" + "^{2}*" + "(?:" + "(?:(?'left'{0}){2}+)+" + "(?:(?'inner-left'{1}){2}*)+" + ")*" + "(?(left)(?!))$" + ")", "\\" + LEFT_BRACKET, "\\" + RIGHT_BRACKET, string.Format("[^\\{0}\\{1}]", LEFT_BRACKET, RIGHT_BRACKET)), RegexOptions.Compiled); this.m_rules = rules.ToIDictionary((item) => item.OperatorSymbol); }
/// <summary> /// Returns a new string in which all occurrences of the specified strings in the current instance are replaced with another specified strings. /// </summary> /// <param name="text"></param> /// <param name="oldValues"></param> /// <param name="newValueSelector"></param> /// <param name="ignoreCase"></param> /// <returns></returns> public static string Replace(this string text, IEnumerable <string> oldValues, Func <string, string> newValueSelector, bool ignoreCase) { if (string.IsNullOrWhiteSpace(text)) { return(text); } oldValues = new List <string>(oldValues.Where((item) => !string.IsNullOrEmpty(item))); if (oldValues.Count() == 0) { return(text); } if (oldValues.Count() == 1) { string oldValue = oldValues.First(); return(Regex.Replace(text, RegexUtility.Encode(oldValue), newValueSelector(oldValue), ignoreCase ? RegexOptions.IgnoreCase : RegexOptions.None)); } //search all occurrences of each old value int groupIndex = 0; StringBuilder pattern = new StringBuilder(); IDictionary <Range, string> replacement = new Dictionary <Range, string>(); foreach (string item in oldValues) { pattern.AppendFormat("({0})|", RegexUtility.Encode(item)); } foreach (Match match in Regex.Matches(text, pattern.Remove(pattern.Length - 1, 1).ToString(), ignoreCase ? RegexOptions.IgnoreCase : RegexOptions.None)) { groupIndex = -1; for (int i = 1; i < match.Groups.Count; i++) { if (match.Groups[i].Success) { groupIndex = i; break; } } replacement[new Range(match.Index, match.Length)] = newValueSelector(oldValues.ElementAt(groupIndex - 1)); } //replace all old values int offset = 0; Range key = null; string value = null; int index = 0, length = 0; StringBuilder result = new StringBuilder(text); foreach (KeyValuePair <Range, string> item in replacement) { key = item.Key; value = item.Value; index = Convert.ToInt32(key.StartIndex) + offset; length = Convert.ToInt32(key.Length); result.Remove(index, length); result.Insert(index, value); offset += value.Length - length; } return(result.ToString()); }