Syntax Highlighting Code

private void richTextBox1_TextChanged(object sender, EventArgs e)
            string tokens = "(auto|double|int|struct|break|else|long|switch|case|enum|register|typedef|char|extern|return|union|const|float|short|unsigned|continue|for|signed|void|default|goto|sizeof|volatile|do|double|if|static|while)";
            Regex rex = new Regex(tokens);
            MatchCollection mc = rex.Matches(richTextBox1.Text);
            int StartCursorPosition = richTextBox1.SelectionStart;
            foreach (Match m in mc)
                int startIndex = m.Index;
                int StopIndex = m.Length;
                richTextBox1.Select(startIndex, StopIndex);
                richTextBox1.SelectionColor = Color.Red;
                richTextBox1.SelectionStart = StartCursorPosition;
                richTextBox1.SelectionColor = Color.Blue;

Prroblem is while do and double has same name starting so it was not working properl so any one provide a good code for me..

7 Years
Discussion Span
Last Post by mcriscolo

You do have the word "double" in your regex string twice (once near the beginning and once near the end). But that doesn't fix the issue.

Modify your token string to look like:

string tokens = "\\s(auto|int|struct|break|else|long|switch|case|enum|register|typedef|char|extern|return|union|const|float|short|unsigned|continue|for|signed|void|default|goto|sizeof|volatile|do|double|if|static|while)\\s";

This will cause it to make sure each token is preceded and followed by a whitespace.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.