How to highlight a word using REGEX

Asked

Viewed 144 times

0

I need to colorize a text in Richtextbox before a string For example:

"Highlight: normal highlight"

  • The text after the : should be interpreted as a regex or as a literal? For example, if the text has a ? it must match with a query (literal) or be interpreted as an optional operator of the previous Pattern (regex)?

  • Only the : that limits the text before, after it any text can be literal.

1 answer

5

See if this can help you

private void ColourRrbText(RichTextBox rtb)
    {
        Regex regExp = new Regex("^([^:]*)");

        foreach (Match match in regExp.Matches(rtb.Text))
        {
            rtb.Select(match.Index, match.Length);
            rtb.SelectionColor = Color.Blue;
        }
    }
  • I needed to do this while the person type, and in Richtextbox it will have several lines and not just one line. Is there any way to do this while the person type or :?

  • 1

    @Sérgiohenrique It would be good if you said that in the question

Browser other questions tagged

You are not signed in. Login or sign up in order to post.