Mithilesh Indurkar
Mithilesh Indurkar

Reputation: 489

Replace characters outside a characterset

I have a string wherein I want to replace some characters which are not matching my required character set to one space each. So if there are 3 characters replaced, there should be 3 spaces.

My required character set =

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789/?().,' "+& 

====> includes space

Code that I have tried so far.

str    = "AC!@+D^*$ASS1@AC!@+D^*$ASS1@AC!@+D^*$ASS1@£ PPP    IE"
expstr = "AC  +D   ASS1 AC  +D   ASS1 AC  +D   ASS1   PPP    IE"

Set objRegX = New RegExp
objRegX.Pattern  = "[^A-Za-z0-9/?().,''+& ']"
str = objRegX.Replace(str, " ")

Please help.

My expected String is the value in the variable 'expstr'. Whereas the 'str' variable is printing the below

AC @+D^*$ASS1@AC!@+D^*$ASS1@AC!@+D^*$ASS1@£ PPP    IE

I need it like the below

AC  +D   ASS1 AC  +D   ASS1 AC  +D   ASS1   PPP    IE

Upvotes: 0

Views: 115

Answers (1)

trincot
trincot

Reputation: 350212

You need to set the Global property to True:

objRegX.Global = True

If you need to exlude the double quote as well, escape it with another double quote, instead of using two single quotes:

objRegX.Pattern  = "[^A-Za-z0-9/?().,""+& ']"

Upvotes: 1

Related Questions