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. Regular Expression Escape Sequence Nightmare!

Regular Expression Escape Sequence Nightmare!

Scheduled Pinned Locked Moved C#
csharpcomregexquestion
13 Posts 4 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.
  • M Offline
    M Offline
    MStanbrook
    wrote on last edited by
    #1

    I've recently been pointed in the direction of RegExps for string replacement. However, being new to both C# and RegExps, and Escape characters are giving me fits! I've got got to remove the following characters from a string (not including the double quotes) "`!@$%^*()+=\|[]{};:<>/?,~" I'm having an awful time trying to evaluate what needs to be "escaped", and how many times!:confused: ugh. Mike Stanbrook mstanbrook@yahoo.com

    N L 3 Replies Last reply
    0
    • M MStanbrook

      I've recently been pointed in the direction of RegExps for string replacement. However, being new to both C# and RegExps, and Escape characters are giving me fits! I've got got to remove the following characters from a string (not including the double quotes) "`!@$%^*()+=\|[]{};:<>/?,~" I'm having an awful time trying to evaluate what needs to be "escaped", and how many times!:confused: ugh. Mike Stanbrook mstanbrook@yahoo.com

      N Offline
      N Offline
      Nnamdi Onyeyiri
      wrote on last edited by
      #2

      "^[a-z0-9_]"

      the above expression should [i think] remove all characters that are not letters, numbers or the underscore, to not remove any other chars, just add them in next to the underscore. p.s. if you dont want to keep underscores, then remove it.


      :suss: Email: theeclypse@hotmail.com   URL: http://www.onyeyiri.co.uk
      :suss:"All programmers are playwrights and all computers are lousy actors."

      1 Reply Last reply
      0
      • M MStanbrook

        I've recently been pointed in the direction of RegExps for string replacement. However, being new to both C# and RegExps, and Escape characters are giving me fits! I've got got to remove the following characters from a string (not including the double quotes) "`!@$%^*()+=\|[]{};:<>/?,~" I'm having an awful time trying to evaluate what needs to be "escaped", and how many times!:confused: ugh. Mike Stanbrook mstanbrook@yahoo.com

        N Offline
        N Offline
        Nnamdi Onyeyiri
        wrote on last edited by
        #3

        MStanbrook wrote: I'm having an awful time trying to evaluate what needs to be "escaped", and how many times! if you mean putting the slash next to them, you can just put the at sign @, before the string, and any special characters will be ignored. :)


        :suss: Email: theeclypse@hotmail.com   URL: http://www.onyeyiri.co.uk
        :suss:"All programmers are playwrights and all computers are lousy actors."

        C M 2 Replies Last reply
        0
        • N Nnamdi Onyeyiri

          MStanbrook wrote: I'm having an awful time trying to evaluate what needs to be "escaped", and how many times! if you mean putting the slash next to them, you can just put the at sign @, before the string, and any special characters will be ignored. :)


          :suss: Email: theeclypse@hotmail.com   URL: http://www.onyeyiri.co.uk
          :suss:"All programmers are playwrights and all computers are lousy actors."

          C Offline
          C Offline
          Christopher Lord
          wrote on last edited by
          #4

          except the " char, which needs to be escaped with a double quote ala vb

          1 Reply Last reply
          0
          • N Nnamdi Onyeyiri

            MStanbrook wrote: I'm having an awful time trying to evaluate what needs to be "escaped", and how many times! if you mean putting the slash next to them, you can just put the at sign @, before the string, and any special characters will be ignored. :)


            :suss: Email: theeclypse@hotmail.com   URL: http://www.onyeyiri.co.uk
            :suss:"All programmers are playwrights and all computers are lousy actors."

            M Offline
            M Offline
            MStanbrook
            wrote on last edited by
            #5

            Here's what I've tried:

            sourceString = "123 ABC X!Y%Z}"

            System.Text.RegularExpressions.Regex.Match(sourceString,"^[a-z0-9_]").ToString()

            which results in: "1" (why only one character?) I've also tried the suggestion:

            System.Text.RegularExpressions.Regex.Match(sourceString,@"`!@$%^*()+=\\|[]{};:<>/?,~").ToString()

            Which results in: error: managed EE does not understand expression's syntax Removing the @ gives a result of "" (Empty string). Mike Stanbrook mstanbrook@yahoo.com

            N 1 Reply Last reply
            0
            • M MStanbrook

              I've recently been pointed in the direction of RegExps for string replacement. However, being new to both C# and RegExps, and Escape characters are giving me fits! I've got got to remove the following characters from a string (not including the double quotes) "`!@$%^*()+=\|[]{};:<>/?,~" I'm having an awful time trying to evaluate what needs to be "escaped", and how many times!:confused: ugh. Mike Stanbrook mstanbrook@yahoo.com

              L Offline
              L Offline
              leppie
              wrote on last edited by
              #6

              Hi First for Regex, Eric Gunnerson has a nice tool (although i have no clue how Regex works :)). Look on the GotDotNet.com website under user samples. Secondly, lets solve this problem :)

              string badchars = @"`!@$%^*()+=\|[]{};:<>/?,~";
              foreach (char bad in badchars) input = input.Replace(bad.ToString(), null );

              Thats it, Hope it helps :) MYrc : A .NET IRC client with C# Plugin Capabilities. See http://sourceforge.net/projects/myrc for more info. :-D

              N M 2 Replies Last reply
              0
              • M MStanbrook

                Here's what I've tried:

                sourceString = "123 ABC X!Y%Z}"

                System.Text.RegularExpressions.Regex.Match(sourceString,"^[a-z0-9_]").ToString()

                which results in: "1" (why only one character?) I've also tried the suggestion:

                System.Text.RegularExpressions.Regex.Match(sourceString,@"`!@$%^*()+=\\|[]{};:<>/?,~").ToString()

                Which results in: error: managed EE does not understand expression's syntax Removing the @ gives a result of "" (Empty string). Mike Stanbrook mstanbrook@yahoo.com

                N Offline
                N Offline
                Nnamdi Onyeyiri
                wrote on last edited by
                #7

                im sorry, the ^ was meant to be inside the brackets, do this

                string sourceString = "123 ABC X!Y%Z}";
                Regex r = new Regex(@"[^a-z0-9]", RegexOptions.IgnoreCase);
                MessageBox.Show(r.Replace(sourceString, ""));


                :suss: Email: theeclypse@hotmail.com   URL: http://www.onyeyiri.co.uk
                :suss:"All programmers are playwrights and all computers are lousy actors."

                L 1 Reply Last reply
                0
                • L leppie

                  Hi First for Regex, Eric Gunnerson has a nice tool (although i have no clue how Regex works :)). Look on the GotDotNet.com website under user samples. Secondly, lets solve this problem :)

                  string badchars = @"`!@$%^*()+=\|[]{};:<>/?,~";
                  foreach (char bad in badchars) input = input.Replace(bad.ToString(), null );

                  Thats it, Hope it helps :) MYrc : A .NET IRC client with C# Plugin Capabilities. See http://sourceforge.net/projects/myrc for more info. :-D

                  N Offline
                  N Offline
                  Nnamdi Onyeyiri
                  wrote on last edited by
                  #8

                  Regex is much better way of doing it, look at my last post above your one to see how it is done with regex. :)


                  :suss: Email: theeclypse@hotmail.com   URL: http://www.onyeyiri.co.uk
                  :suss:"All programmers are playwrights and all computers are lousy actors."

                  1 Reply Last reply
                  0
                  • N Nnamdi Onyeyiri

                    im sorry, the ^ was meant to be inside the brackets, do this

                    string sourceString = "123 ABC X!Y%Z}";
                    Regex r = new Regex(@"[^a-z0-9]", RegexOptions.IgnoreCase);
                    MessageBox.Show(r.Replace(sourceString, ""));


                    :suss: Email: theeclypse@hotmail.com   URL: http://www.onyeyiri.co.uk
                    :suss:"All programmers are playwrights and all computers are lousy actors."

                    L Offline
                    L Offline
                    leppie
                    wrote on last edited by
                    #9

                    Thanx, I like to see snippets :) MYrc : A .NET IRC client with C# Plugin Capabilities. See http://sourceforge.net/projects/myrc for more info. :-D

                    N 1 Reply Last reply
                    0
                    • L leppie

                      Thanx, I like to see snippets :) MYrc : A .NET IRC client with C# Plugin Capabilities. See http://sourceforge.net/projects/myrc for more info. :-D

                      N Offline
                      N Offline
                      Nnamdi Onyeyiri
                      wrote on last edited by
                      #10

                      Dont we all :-D


                      :suss: Email: theeclypse@hotmail.com   URL: http://www.onyeyiri.co.uk
                      :suss:"All programmers are playwrights and all computers are lousy actors."

                      1 Reply Last reply
                      0
                      • L leppie

                        Hi First for Regex, Eric Gunnerson has a nice tool (although i have no clue how Regex works :)). Look on the GotDotNet.com website under user samples. Secondly, lets solve this problem :)

                        string badchars = @"`!@$%^*()+=\|[]{};:<>/?,~";
                        foreach (char bad in badchars) input = input.Replace(bad.ToString(), null );

                        Thats it, Hope it helps :) MYrc : A .NET IRC client with C# Plugin Capabilities. See http://sourceforge.net/projects/myrc for more info. :-D

                        M Offline
                        M Offline
                        MStanbrook
                        wrote on last edited by
                        #11

                        HAHA!:) That was my original approach!! It was suggested that a "better way" was using RegExps. (My example at that time was stripping "{","-", and "}" from GUIDs. Funny how some things come full circle! (note: I wasn't able to find Eric's tool, but there was a RegEx Workbench that might do the trick). Thanks. Mike Stanbrook mstanbrook@yahoo.com

                        M 1 Reply Last reply
                        0
                        • M MStanbrook

                          HAHA!:) That was my original approach!! It was suggested that a "better way" was using RegExps. (My example at that time was stripping "{","-", and "}" from GUIDs. Funny how some things come full circle! (note: I wasn't able to find Eric's tool, but there was a RegEx Workbench that might do the trick). Thanks. Mike Stanbrook mstanbrook@yahoo.com

                          M Offline
                          M Offline
                          MStanbrook
                          wrote on last edited by
                          #12

                          MStanbrook wrote: (note: I wasn't able to find Eric's tool, but there was a RegEx Workbench that might do the trick). I'm a spazz.:eek: The RegEx Workbench *IS* Eric's tool. Mike Stanbrook mstanbrook@yahoo.com

                          L 1 Reply Last reply
                          0
                          • M MStanbrook

                            MStanbrook wrote: (note: I wasn't able to find Eric's tool, but there was a RegEx Workbench that might do the trick). I'm a spazz.:eek: The RegEx Workbench *IS* Eric's tool. Mike Stanbrook mstanbrook@yahoo.com

                            L Offline
                            L Offline
                            leppie
                            wrote on last edited by
                            #13

                            Sorry , i was gonna reply but it slipped my mind, glad you made the association :) MYrc : A .NET IRC client with C# Plugin Capabilities. See http://sourceforge.net/projects/myrc for more info. :-D

                            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