sansactions
sansactions

Reputation: 243

Error on replacing special characters with regex function

I am trying to replace the special characters and spaces in a string. I found online that a regex function is handy for this, but I can't seem to make it work.

When I try this the regex function throws an error "expected end of instruction":

Dim param = Regex.Replace("te  !,;stDD ", "[^A-Za-z0-9]", "")

When I try this the string is not changed but the regex function does pass:

Set re = New RegExp
re.Pattern = "[^A-Za-z0-9]"
param = re.Replace("te  !,;stDD ", "")  'string doesn't change on result

Upvotes: 0

Views: 961

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200543

When I try this the regex function throws an error "expected end of instruction":

Dim param = Regex.Replace("te  !,;stDD ", "[^A-Za-z0-9]", "")

The error is raised by the parser, not the regex method. In VBScript you cannot define a variable and assign a value to it in the same step. You MUST separate definition and assignment (if you want an explicit definition).

Dim param
param = Regex.Replace("te  !,;stDD ", "[^A-Za-z0-9]", "")

You can put both statements in one line by separating them with a colon, though:

Dim param : param = Regex.Replace("te  !,;stDD ", "[^A-Za-z0-9]", "")

Note, however, that there is no Regex class with a static Replace() method in VBScript, so you'd still be getting an error from Regex.Replace().

You must instantiate a RegExp object like you do in your second code snippet and use the Replace() method of that object.

When I try this the string is not changed but the regex function does pass:

Set re = New RegExp
re.Pattern = "[^A-Za-z0-9]"
param = re.Replace("te  !,;stDD ", "")   'string doesn't change on result

But the string does change. If you take a closer look at input and output you'll see:

"te  !,;stDD "      ← input string
"te !,;stDD "       ← output string

Your replacement operation removes the first character that is neither letter nor digit from the string. For your example string that is the first space. In order to remove all characters that aren't letters or digits you need to set the Global property to True:

Set re = New RegExp
re.Pattern = "[^A-Za-z0-9]"
re.Global  = True
param = re.Replace("te  !,;stDD ", "")

Upvotes: 3

Related Questions