regex problem

Kodeeswaran Duraisamy 161 Reputation points
2020-09-09T09:24:22.65+00:00

Why this is not working
dim litem as string = "abc-12//by][" & chr(34) & "name"

System.Text.RegularExpressions.Regex.Replace(Litem, "'/.,<>=+|-)( :;~!@#$%^&*`_?[]"" ", "").ToUpper

My expected the result is - ABC12BYNAME

but why error? What is my mistake and what is the solution?

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,604 questions
{count} votes

Accepted answer
  1. Viorel 112.1K Reputation points
    2020-09-09T12:14:54.977+00:00

    If you are also interested in the problems of your first approach, then consider the next aspects.

    If you want to change the variable, use assignment:

    litem = Regex.Replace(litem, . . .

    If you want to find any of listed characters, use “[…]”.

    Make sure that the characters, which also have a special meaning in regular expressions, are preceded with “\”. Something like this:

    litem = Regex.Replace(litem, "['/.,<>=+|\-)( :;~!@#$%\^&*_?[\]"" ]", "").ToUpper()

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Castorix31 81,721 Reputation points
    2020-09-09T09:57:33.12+00:00

    You can do :

    Dim sResult As String = Regex.Replace(litem, "[^a-zA-Z0-9]", String.Empty).ToUpper()
    
    1 person found this answer helpful.