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. Help with a search & replace

Help with a search & replace

Scheduled Pinned Locked Moved C#
regexquestionalgorithmshelp
3 Posts 3 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.
  • R Offline
    R Offline
    rbuchana
    wrote on last edited by
    #1

    I have a text box & a property window. The text box is read-only and displays C code. As the users change properties the C-code changes respectively. I've implemented this using Regex's replace function. However it is really really slow. Here's what I have

            private void updateCCodeTextBoxWorker(int address, string name)
            {
                cCodeTextBox.SuspendLayout();
                
                string pattern = name + @", 0x..";
                string replace = String.Format("{0}, 0x{1:X2}", name, regData[address]);
                Regex rgx = new Regex(pattern);
    
                cCodeTextBox.Text = rgx.Replace(cCodeTextBox.Text, replace);
    
                cCodeTextBox.ResumeLayout();
            }
    

    The string "name" is basically what I'm searching for and "regData[address]" is the value that's changing. Is there a better way to do this? How can I make it faster? Thanks, --RB

    L L 2 Replies Last reply
    0
    • R rbuchana

      I have a text box & a property window. The text box is read-only and displays C code. As the users change properties the C-code changes respectively. I've implemented this using Regex's replace function. However it is really really slow. Here's what I have

              private void updateCCodeTextBoxWorker(int address, string name)
              {
                  cCodeTextBox.SuspendLayout();
                  
                  string pattern = name + @", 0x..";
                  string replace = String.Format("{0}, 0x{1:X2}", name, regData[address]);
                  Regex rgx = new Regex(pattern);
      
                  cCodeTextBox.Text = rgx.Replace(cCodeTextBox.Text, replace);
      
                  cCodeTextBox.ResumeLayout();
              }
      

      The string "name" is basically what I'm searching for and "regData[address]" is the value that's changing. Is there a better way to do this? How can I make it faster? Thanks, --RB

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Have you tried the RegexOptions.Compiled flag? It may help, since the search pattern will be compiled into the assembly, which yields in faster execution time regards

      1 Reply Last reply
      0
      • R rbuchana

        I have a text box & a property window. The text box is read-only and displays C code. As the users change properties the C-code changes respectively. I've implemented this using Regex's replace function. However it is really really slow. Here's what I have

                private void updateCCodeTextBoxWorker(int address, string name)
                {
                    cCodeTextBox.SuspendLayout();
                    
                    string pattern = name + @", 0x..";
                    string replace = String.Format("{0}, 0x{1:X2}", name, regData[address]);
                    Regex rgx = new Regex(pattern);
        
                    cCodeTextBox.Text = rgx.Replace(cCodeTextBox.Text, replace);
        
                    cCodeTextBox.ResumeLayout();
                }
        

        The string "name" is basically what I'm searching for and "regData[address]" is the value that's changing. Is there a better way to do this? How can I make it faster? Thanks, --RB

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Hi, So for every property change, the code executes, creates a new Regex and makes it Replace something. Regex is an expensive class by itself, it has to either interpret your intentions, or, optionally generate compiled code for it (more cost effective only when required over and over, not your case). If the user types 5 characters for a property, it changes five times. I would look for a way to run the code less frequently. Some ideas: - let the user trigger the code by clicking a button or so (less comfortable); - let a timer trigger the code; maybe have a scheme where the worker runs say 3 seconds after the last change (i.e. every change starts/restarts a timer, and only when the 3 seconds elapse is the code executed). Hope this helps :)

        Luc Pattyn


        try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


        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