void ColorCharacters(RichTextBox rtf, string fromText, string stringToColor, Color color)
{
int posInText = 0;
if (stringToColor.Trim() == "")
{
rtf.SelectionBackColor = Color.AliceBlue;
rtf.AppendText(fromText);
}
else
{
string from = fromText;
Regex pattern = new Regex($"{stringToColor.Trim()}",RegexOptions.IgnoreCase);
Match match = pattern.Match(from);
while (match.Success)
{
// output not outputted text
if ((match.Index - posInText) > 0)
{
rtf.SelectionBackColor = Color.AliceBlue;
rtf.AppendText(from.Substring(posInText, match.Index - posInText));
}
rtf.SelectionBackColor = Color.Gold;
rtf.AppendText(match.Value);
posInText = match.Index + match.Length;
match = match.NextMatch();
}
if ((from.Length - 1 - posInText) >= 0)
{
rtf.SelectionBackColor = Color.AliceBlue;
rtf.SelectionBackColor = Color.AliceBlue;
rtf.AppendText(from.Substring(posInText, from.Length - posInText));
}
}
}