SgDotNet
Singapore Professional .NET User Group -For Cool Developers

Code snippet - 0008

Latest post 04-22-2005 5:10 PM by ericwsw. 3 replies.
  • 04-22-2005 8:45 AM

    • ericwsw
    • Top 50 Contributor
    • Joined on 05-04-2004
    • Singapore
    • Posts 82

    Code snippet - 0008

           Filter out unwanted characters in the string

          
            Dim a, b As String
            a = "123 % Hello $ 456 world 789 $"

            ' b = "123 Hello 456 world 789"
            b = System.Text.RegularExpressions.Regex.Replace(a, "[^\w\s]", "").Trim
            b = System.Text.RegularExpressions.Regex.Replace(b, "\s+", " ")


            ' b = "12345678"
            b = System.Text.RegularExpressions.Regex.Replace(a, "[^\d]", "")
            b = System.Text.RegularExpressions.Regex.Replace(b, "\s+", " ")


            ' b = "Hello world"
            b = System.Text.RegularExpressions.Regex.Replace(a, "[^A-Za-z ]", "").Trim
            b = System.Text.RegularExpressions.Regex.Replace(b, "\s+", " ")


     

  • 04-22-2005 4:29 PM In reply to

    Re: Code snippet - 0008

    Many people do not understand Regular Expressions so you may want to comment just what you code is doing.

    The melody of logic will always play out the truth. ~ Narumi Ayumu, Spiral

  • 04-22-2005 4:58 PM In reply to

    • ericwsw
    • Top 50 Contributor
    • Joined on 05-04-2004
    • Singapore
    • Posts 82

    Re: Code snippet - 0008

    Thank you for the feedback Smile [:)]
  • 04-22-2005 5:10 PM In reply to

    • ericwsw
    • Top 50 Contributor
    • Joined on 05-04-2004
    • Singapore
    • Posts 82

    Re: Code snippet - 0008

    The expression \w includes all letters and numbers, the \s all spaces. The ^ negates the expression, so all invalid characters get replaced with an empty string.

    The expression  \d  includes all decimal digit. The ^ negates the expression, so all invalid decimal digit get replaced with an empty string.

    The expression  \s+  includes one or more spaces, so regardless one or more spaces get replaced with a space

     

Page 1 of 1 (4 items) | RSS
Copyright SgDotNet 2004-2008
Powered by Community Server (Commercial Edition), by Telligent Systems