RegExp Pattern Eluding Me

C

Colin Steadman

I'm trying to setup a function that will remove any
character from a string which is NOT either a space, a-z
or 0-9. I'm using a Regular Expression to do this.

I'm almost there but I cant get the pattern right. After
trying all sorts of crazy patterns the closest I've come
is with this simplistic attempt:


Function ripOutSpecialCharacters(strComment)
Set re = New RegExp
re.pattern = "\W"
re.global = True
ripOutSpecialCharacters = re.replace(strComment, " ")
Set re = Nothing
End Function


This is almost what I want, but if I run it on a string
like this -

Its that special blend and
roast.

I end up with this -

Its that special blend and roast.
^^ ie two spaces here.

I'm really struggling to get the pattern right, can anyone
offer any help on this?

TIA,

Colin
 
C

Colin Steadman

I think I've done enough to satisfy myself. My function
now looks like this -

Function ripOutSpecialCharacters(strComment)
Set re = New RegExp
re.pattern = "\r\n"
re.global = True
ripOutSpecialCharacters = re.replace(strComment, "")
Set re = Nothing
End Function

This is dealing with the carriage returns I was trying to
get rid of, although I'm not sure why \r cant be used on
its own?

Colin
 
K

keith

If you truly want all characters "which [are] NOT either a
space, a-z or 0-9"

Try

\w Matches any word character including underscore.
Equivalent to "[A-Za-z0-9_]".

OR

\W Matches any non-word character. Equivalent to "[^A-Za-
z0-9_]".

Although you may have to add, or remove, something if you
want to exclude underscores, but it is more all
encompassing than just replacing \r and \n.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top