0
votes

I have this code to apply basic syntax highlighting to a RichTextBox

    public Regex _KeyWords = new Regex ("event|explicit|extern|false|finally|fixed|float|for");

    private void button1_Click(object sender, EventArgs e)
    {
        int _SelPos = richTextBox1.SelectionStart;
        foreach (Match keyWordMatch in _KeyWords.Matches(richTextBox1.Text))
        {
            richTextBox1.Select(keyWordMatch.Index, keyWordMatch.Length);
            richTextBox1.SelectionColor = Color.Blue;
            richTextBox1.SelectionStart = _SelPos;
            richTextBox1.SelectionColor = Color.Black;
        }
    }

It works fine, but i can't find a way to highlight only the text contained between two tags, for example:

<tagStart>
public Form1()
{
   InitializeComponent();
}
<tagEnd>

I think it can be done with Regex but i don't have that much experience with it, so any help will be appreciated.

1

1 Answers

1
votes

This regex should work.

(?<=<tagStart>)(.*)(?=<tagEnd>)

GSkinner example

Credit