Results 1 to 5 of 5

Thread: Replace Comma with Regex.Replace

  1. #1
    RadioRaiders is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Oct 2008
    Posts
    6
    Reputation
    0

    Question Replace Comma with Regex.Replace

    I am trying to filter out all unwanted characters from a string. All I want in the string is letters A-Z, numbers 0-9 as well as comma (,) plus (+) and quotes (").

    I figured how to do the letters and numbers, but the 3 special characters are giving me a problem. I also tried with the Chr(34) equivelant for the " sign, but no luck, as and " or , messes up the way the code is read.

    Here´s what I have so far, but it only works for A-Z and 0-9:
    STR = System.Text.RegularExpressions.Regex.Replace(STR, "[^A-Z, 0-9, Chr(43) ]", "")
    Any ideas?

    Last edited by RadioRaiders; 03-10-2009 at 8:11 AM.

  2. #2
    Tom
    Tom is offline VB.NET Forum Idol
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Aug 2005
    Posts
    746
    Reputation
    385
    How to: Strip Invalid Characters from a String

    The following code example uses the static Regex..::.Replace method to strip invalid characters from a string. You can use the CleanInput method defined here to strip potentially harmful characters that have been entered into a text field in a form that accepts user input. CleanInput returns a string after stripping out all nonalphanumeric characters except @, - (a dash), and . (a period).

    Code:
     Public Function CleanInput(strIn As String) As String
    
            ' Replace invalid characters with empty strings.
            Return Regex.Replace(strIn, "[^\w\.@-]", "")
    
        End Function

  3. #3
    RadioRaiders is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Oct 2008
    Posts
    6
    Reputation
    0
    Quote Originally Posted by Tom View Post
    How to: Strip Invalid Characters from a String

    The following code example uses the static Regex..::.Replace method to strip invalid characters from a string. You can use the CleanInput method defined here to strip potentially harmful characters that have been entered into a text field in a form that accepts user input. CleanInput returns a string after stripping out all nonalphanumeric characters except @, - (a dash), and . (a period).

    Code:
     Public Function CleanInput(strIn As String) As String
    
            ' Replace invalid characters with empty strings.
            Return Regex.Replace(strIn, "[^\w\.@-]", "")
    
        End Function
    Thanks. But I'm still having a problem leaving in " and , as if I add them to the list along with the ."- it corrupts the line.

    Is there anyway I can use this regex.raplace using the Chr() code for the ASCII characters? ex. I would like to exclude all characters outside the range of Chr(33) to Chr(125)

  4. #4
    cjard's Avatar
    cjard is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2006
    Posts
    7,036
    Reputation
    1716
    Noone ever told you how to put a speechmark " into a string in VB?

    Code:
    Public Function CleanInput(strIn As String) As String
    
            ' Replace invalid characters with empty strings.
            Return Regex.Replace(strIn, "[^A-Z0-9,+""]", "")
    
        End Function
    Note the double speechmark "" which does not 'break the string' because VB understands to escape this to a single speechmark

    The following code MAY match outside Chr 33 to Chr 125 and replace it with nothing:

    Code:
    Public Function CleanInput(strIn As String) As String
    
            ' Replace invalid characters with empty strings.
            Return Regex.Replace(strIn, "[^\u0021-\u007D]", "")
    
        End Function
    I say may because I've never really used the unicode specifiers; try it

  5. #5
    cjard's Avatar
    cjard is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2006
    Posts
    7,036
    Reputation
    1716
    Without using regex, something like:

    Code:
    Public Function CleanInput(strIn As String) As String
      Dim sb as New StringBuilder
      Dim lo as Char = Chr(32)
      Dim hi as Char = Chr(126)
    
      ForEach c as Char in strIn
    
        If c > lo AndAlso c < hi Then sb.Append(c)
    
      Next
    
      Return sb.ToString()
    
        End Function

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking