Replace Comma with Regex.Replace

RadioRaiders

Member
Joined
Oct 28, 2008
Messages
6
Programming Experience
Beginner
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?

ascii_table.jpg
 
Last edited:
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).

VB.NET:
    [COLOR="Blue"]Public Function [/COLOR]CleanInput(strIn As String) [COLOR="blue"]As String[/COLOR]

        [COLOR="SeaGreen"]' Replace invalid characters with empty strings.[/COLOR]
        [COLOR="blue"]Return [/COLOR]Regex.Replace(strIn, "[^\w\.@-]", "")

    [COLOR="blue"]End Function[/COLOR]
 
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).

VB.NET:
    [COLOR="Blue"]Public Function [/COLOR]CleanInput(strIn As String) [COLOR="blue"]As String[/COLOR]

        [COLOR="SeaGreen"]' Replace invalid characters with empty strings.[/COLOR]
        [COLOR="blue"]Return [/COLOR]Regex.Replace(strIn, "[^\w\[B][COLOR="Red"].@-[/COLOR][/B]]", "")

    [COLOR="blue"]End Function[/COLOR]
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)
 
Noone ever told you how to put a speechmark " into a string in VB? :(

VB.NET:
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:

VB.NET:
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
 
Without using regex, something like:

VB.NET:
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
 
Back
Top