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. replacement regular expression

replacement regular expression

Scheduled Pinned Locked Moved C#
regexquestion
10 Posts 5 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.
  • P Offline
    P Offline
    Pyro Joe
    wrote on last edited by
    #1

    hi, I need a regular expression to replace / with // in a richTextBox1. how is this done? I have this: string replace_1 = Regex.Replace(richTextBox1.Text, @"/", @"//"); but it doesn't replace the string with the replacement string in the richTextBox1. can you show me something that would work? thanks in advance, Stephen

    C 1 Reply Last reply
    0
    • P Pyro Joe

      hi, I need a regular expression to replace / with // in a richTextBox1. how is this done? I have this: string replace_1 = Regex.Replace(richTextBox1.Text, @"/", @"//"); but it doesn't replace the string with the replacement string in the richTextBox1. can you show me something that would work? thanks in advance, Stephen

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Why do you need a regex here ? The string class is well able to do this for you. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

      P 1 Reply Last reply
      0
      • C Christian Graus

        Why do you need a regex here ? The string class is well able to do this for you. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

        P Offline
        P Offline
        Pyro Joe
        wrote on last edited by
        #3

        can you show me what the code would look like. I tried this: string replace_1 = richTextBox1.Text; replace_1.Replace("/", "//"); richTextBox1.Text = ""; richTextBox1.Text = replace_1; but no luck.

        C D 2 Replies Last reply
        0
        • P Pyro Joe

          can you show me what the code would look like. I tried this: string replace_1 = richTextBox1.Text; replace_1.Replace("/", "//"); richTextBox1.Text = ""; richTextBox1.Text = replace_1; but no luck.

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          Have you looked in the debugger ? I think the rich text box is your problem. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

          1 Reply Last reply
          0
          • P Pyro Joe

            can you show me what the code would look like. I tried this: string replace_1 = richTextBox1.Text; replace_1.Replace("/", "//"); richTextBox1.Text = ""; richTextBox1.Text = replace_1; but no luck.

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            RichTextBox1.Text.Replace(@"/", @"//");

            You're going to run into a problem each time you run this though. Every time you run this, or any other similar function, you'll end up doubling the number of slash marks EVERY time it's run. So...

            / This is a test...

            will become

            // This is a test...

            will become

            //// This is a test...

            will become

            //////// This is a test...

            Unless you put in some code that checks for this condition... RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

            P 1 Reply Last reply
            0
            • D Dave Kreskowiak

              RichTextBox1.Text.Replace(@"/", @"//");

              You're going to run into a problem each time you run this though. Every time you run this, or any other similar function, you'll end up doubling the number of slash marks EVERY time it's run. So...

              / This is a test...

              will become

              // This is a test...

              will become

              //// This is a test...

              will become

              //////// This is a test...

              Unless you put in some code that checks for this condition... RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

              P Offline
              P Offline
              Pyro Joe
              wrote on last edited by
              #6

              I'm having horrible luck with this. i made a simple simple app with one regular textBox1, and a button. on the button's click I have this code: textBox1.Text.Replace(@"/", @"//"); just like you showed me. it still won't work (I also tried it with the richTextBox, and still no luck. I'm so annoyed at this. It's plagueing me. any ideas? thanks

              Y M 2 Replies Last reply
              0
              • P Pyro Joe

                I'm having horrible luck with this. i made a simple simple app with one regular textBox1, and a button. on the button's click I have this code: textBox1.Text.Replace(@"/", @"//"); just like you showed me. it still won't work (I also tried it with the richTextBox, and still no luck. I'm so annoyed at this. It's plagueing me. any ideas? thanks

                Y Offline
                Y Offline
                Yulianto
                wrote on last edited by
                #7

                Try this

                str="aa/aaa/aaa/"
                str1=””
                Iterate each character in str
                {
                	if TheCharacter =="/" 
                	  str1+="/"
                        str1+=TheCharacter
                }
                

                Work hard, Work effectively and a bit of luck is the key to success.
                1 Reply Last reply
                0
                • P Pyro Joe

                  I'm having horrible luck with this. i made a simple simple app with one regular textBox1, and a button. on the button's click I have this code: textBox1.Text.Replace(@"/", @"//"); just like you showed me. it still won't work (I also tried it with the richTextBox, and still no luck. I'm so annoyed at this. It's plagueing me. any ideas? thanks

                  M Offline
                  M Offline
                  mav northwind
                  wrote on last edited by
                  #8

                  Hi! It's quite simple, strange that no-one noticed so far. string.Replace(a,b) returns a new string holding the text after the replacement. So if you simply write:

                  richTextBox1.Text = richTextBox1.Text.Replace("/","//");

                  you should be fine. Regards, mav

                  D 1 Reply Last reply
                  0
                  • M mav northwind

                    Hi! It's quite simple, strange that no-one noticed so far. string.Replace(a,b) returns a new string holding the text after the replacement. So if you simply write:

                    richTextBox1.Text = richTextBox1.Text.Replace("/","//");

                    you should be fine. Regards, mav

                    D Offline
                    D Offline
                    Dave Kreskowiak
                    wrote on last edited by
                    #9

                    :doh: Smack! What a bad day yesterday... RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                    P 1 Reply Last reply
                    0
                    • D Dave Kreskowiak

                      :doh: Smack! What a bad day yesterday... RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                      P Offline
                      P Offline
                      Pyro Joe
                      wrote on last edited by
                      #10

                      awesome!!! what a relief. thanks guys!

                      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