Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > RegExp Pattern Eluding Me

Reply
Thread Tools

RegExp Pattern Eluding Me

 
 
Colin Steadman
Guest
Posts: n/a
 
      11-26-2003
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
 
Reply With Quote
 
 
 
 
Colin Steadman
Guest
Posts: n/a
 
      11-26-2003
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
 
Reply With Quote
 
 
 
 
keith
Guest
Posts: n/a
 
      11-26-2003
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.



>-----Original Message-----
>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
>.
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
new RegExp().test() or just RegExp().test() Matěj Cepl Javascript 3 11-24-2009 02:41 PM
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
Ruby 1.9 - ArgumentError: incompatible encoding regexp match(US-ASCII regexp with ISO-2022-JP string) Mikel Lindsaar Ruby 0 03-31-2008 10:27 AM
Programmatically turning a Regexp into an anchored Regexp Greg Hurrell Ruby 4 02-14-2007 06:56 PM
RegExp.exec() returns null when there is a match - a JavaScript RegExp bug? Uldis Bojars Javascript 2 12-17-2006 09:59 PM



Advertisments