/// <summary> /// Replaces all matches of the given regular expression with the replacement value using the specified formatting. /// </summary> /// <param name="regex">The regular expression to find.</param> /// <param name="toReplace">The replacement value</param> /// <param name="foregroundColor">The foreground color (defaults to the console's foreground color at initialization time).</param> /// <param name="backgroundColor">The background color (defaults to the console's background color at initialization time).</param> /// <returns></returns> public ConsoleString ReplaceRegex(string regex, string toReplace, ConsoleColor?foregroundColor = null, ConsoleColor?backgroundColor = null) { ConsoleString ret = new ConsoleString(this); MatchCollection matches = Regex.Matches(this.ToString(), regex); foreach (Match match in matches) { ret = ret.Replace(match.Value, toReplace ?? match.Value, foregroundColor, backgroundColor); } return(ret); }