Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Experiment -- Looking for suggestions

Experiment -- Looking for suggestions

Scheduled Pinned Locked Moved C#
regexhtmldatabasediscussionlearning
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    simplicitylabs
    wrote on last edited by
    #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;
            }
        }
    
    M 1 Reply Last reply
    0
    • S simplicitylabs

      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;
              }
          }
      
      M Offline
      M Offline
      Martin 0
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups