/// <summary> /// Replaces the specified input. /// </summary> /// <param name="input">The input.</param> /// <param name="regularExpression">The regular expression.</param> /// <param name="captureIndex">Index of the capture.</param> /// <param name="replacement">The replacement.</param> /// <returns></returns> public static string Replace(string input, string regularExpression, int captureIndex, CallbackRegexReplacement replacement) { Match m = Regex.Match(input, regularExpression); StringBuilder result = new StringBuilder(input.Length); while (m.Success) { Group g = m.Groups[captureIndex]; string[] pieces = StringUtilities.SplitOn(input, g.Index, false); result.Append(pieces[0]); result.Append(replacement(captureIndex, g.ToString())); input = pieces[1].Substring(g.Length); // Get the next match m = Regex.Match(input, regularExpression); } result.Append(input); return(result.ToString()); }
/// <summary> /// Replaces the specified input. /// </summary> /// <param name="input">The input.</param> /// <param name="regularExpression">The regular expression.</param> /// <param name="captureIndex">Index of the capture.</param> /// <param name="replacement">The replacement.</param> /// <returns></returns> public static string Replace(string input, string regularExpression, int captureIndex, CallbackRegexReplacement replacement) { Match m = Regex.Match(input, regularExpression); StringBuilder result = new StringBuilder(input.Length); while (m.Success) { Group g = m.Groups[captureIndex]; string[] pieces = StringUtilities.SplitOn(input, g.Index, false); result.Append(pieces[0]); result.Append(replacement(captureIndex, g.ToString())); input = pieces[1].Substring(g.Length); // Get the next match m = Regex.Match(input, regularExpression); } result.Append(input); return result.ToString(); }