Experiment -- Looking for suggestions
-
While this may not be best practice, it's experimental in my attempts to learn regex while playing around with a RichTextBox. The idea is to find all HTML tags and color them. However, it's not behaving well. It does indeed find tags such as <html> or </html>, but not <body bgcolor="#ffffff">. I'm relatively sure I have the Regular Expression correct. Another strange behavior is it won't let me edit the colored tags once they have been "colorized". I'm triggering this with a key press event (Enter). At this point, I'm open to suggestions to make this work as prescribed, and of course, I'm prepared to hear alternative suggestions to get the results I'm looking for. Thank you...
private void ChangeTagColors(RichTextBox richTextBox1) { richTextBox1.SelectionStart = 0; richTextBox1.SelectionLength = richTextBox1.Text.Length; int index = 0; foreach (string str in richTextBox1.Text.Split()) { Regex regex = new Regex( @"(\\<\[^\\>\]\*\\>)", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.ExplicitCapture | RegexOptions.Compiled ); if (regex.IsMatch(str)) { richTextBox1.SelectionColor = Color.Blue; richTextBox1.SelectionProtected = true; } index = index + str.Length + 1; } }
-
While this may not be best practice, it's experimental in my attempts to learn regex while playing around with a RichTextBox. The idea is to find all HTML tags and color them. However, it's not behaving well. It does indeed find tags such as <html> or </html>, but not <body bgcolor="#ffffff">. I'm relatively sure I have the Regular Expression correct. Another strange behavior is it won't let me edit the colored tags once they have been "colorized". I'm triggering this with a key press event (Enter). At this point, I'm open to suggestions to make this work as prescribed, and of course, I'm prepared to hear alternative suggestions to get the results I'm looking for. Thank you...
private void ChangeTagColors(RichTextBox richTextBox1) { richTextBox1.SelectionStart = 0; richTextBox1.SelectionLength = richTextBox1.Text.Length; int index = 0; foreach (string str in richTextBox1.Text.Split()) { Regex regex = new Regex( @"(\\<\[^\\>\]\*\\>)", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.ExplicitCapture | RegexOptions.Compiled ); if (regex.IsMatch(str)) { richTextBox1.SelectionColor = Color.Blue; richTextBox1.SelectionProtected = true; } index = index + str.Length + 1; } }
Hello,
simplicitylabs wrote:
but not
Maybe it's because of the " chars, try: You also could simplify your regex like this: @"(\<.*\>)" Apart from that I would suggest you to use the String class methods for this kind of simple validation (IndexOf). I now that regex is extremely powerfull, but it's also very expensive in terms of perfomance. Shortly I made a performance test because I had to validate a lot of strings very often in my application. I found out that using 3 IndexOf instead of a regex was much faster (more than 100 times faster).
simplicitylabs wrote:
Another strange behavior is it won't let me edit the colored tags once they have been "colorized".
I think it's not strange at all, as you are explicitly forcing it with: richTextBox1.SelectionProtected = true; Hope it helps! All the best, Martin