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..

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.