Syntax Highlighting Trouble
I'm trying to make a program that I can use which will highlight certains keywords in the script a certain color. This works fine if the file is relatively small, but once the file gets to be around 1 megabyte in size, it takes nearly 5-10 minutes which is not really plausible. I have used other programs, such as EditPlus, which parse a whole file, no matter the size, in a matter of seconds. Does anyone know of a better way to do syntax highlighting stuff? (Note: the text box is simply a RichTextBox control)
I'm trying to make a program that I can use which will highlight certains keywords in the script a certain color. This works fine if the file is relatively small, but once the file gets to be around 1 megabyte in size, it takes nearly 5-10 minutes which is not really plausible. I have used other programs, such as EditPlus, which parse a whole file, no matter the size, in a matter of seconds. Does anyone know of a better way to do syntax highlighting stuff? (Note: the text box is simply a RichTextBox control)
Code:
private void ParseText()
{
string text = this.scriptText.Text;
foreach (string myString in Globals.SyntaxColor.RedKeys)
{
Regex matchCheck = new Regex(myString);
int numMatches = matchCheck.Matches(text).Count;
foreach (Match match in matchCheck.Matches(text))
{
scriptText.SelectionStart = match.Index;
scriptText.SelectionLength = match.Length;
scriptText.SelectionColor = Color.Red;
}
}
foreach (string myString in Globals.SyntaxColor.BlueKeys)
{
Regex matchCheck = new Regex(myString);
int numMatches = matchCheck.Matches(text).Count;
foreach (Match match in matchCheck.Matches(text))
{
scriptText.SelectionStart = match.Index;
scriptText.SelectionLength = match.Length;
scriptText.SelectionColor = Color.Red;
}
}
foreach (string myString in Globals.SyntaxColor.GreenKeys)
{
Regex matchCheck = new Regex(myString);
int numMatches = matchCheck.Matches(text).Count;
foreach (Match match in matchCheck.Matches(text))
{
scriptText.SelectionStart = match.Index;
scriptText.SelectionLength = match.Length;
scriptText.SelectionColor = Color.Red;
}
}
scriptText.SelectionStart = 0;
scriptText.SelectionLength = 0;
}