• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 641
  • Last Modified:

Regular Expressions - Replace all non letter and numbers except the space

I have the following function.  It replaces all non letters and non numbers.  But, it also replaces the space.  I don't want it to replace the space.  What regular expression would I use?  Thanks in advance


Function ReplaceNonCharAndNonNum(strToReplace,strPattern,strToReplaceWith)
    Dim oRegExp
	Set oRegExp = New RegExp
	oRegExp.Pattern = vPattern
	oRegExp.IgnoreCase = True
	oRegExp.Global = True
	kLeachRegExp = oRegExp.Replace(vStr1, vStr2)
	Set oRegExp = Nothing
End Function
 
vstr = ReplaceNonCharAndNonNum("The quick brown fox jumped over the lazy dog12 *&sd1299 8.", "[^a-z0-9]+", "")

Open in new window

0
cartch2008
Asked:
cartch2008
  • 3
1 Solution
 
CCongdonCommented:
Try this
 [\W-[ \s]]
It means 'Match any non-word character that is not a whitespace character'
0
 
CCongdonCommented:
Or if you feel more comfortable keeping something closer to your original:
[^a-z0-9\s]+
0
 
käµfm³d 👽Commented:
How about:
Function ReplaceNonCharAndNonNum(strToReplace,strPattern,strToReplaceWith)
    Dim oRegExp
    
    Set oRegExp = New RegExp
    oRegExp.Pattern = strPattern
    oRegExp.IgnoreCase = True
    oRegExp.Global = True
    kLeachRegExp = oRegExp.Replace(strToReplace, strToReplaceWith)
    Set oRegExp = Nothing
 
    ReplaceNonCharAndNonNum = kLeachRegExp
End Function
 
vstr = ReplaceNonCharAndNonNum("The quick brown fox jumped over the lazy dog12 *&sd1299 8.", "[^a-z0-9\s]+", "")
 
MsgBox vstr

Open in new window

0
 
cartch2008Author Commented:
This didn't work:   [\W-[ \s]]

But this did:  [^a-z0-9\s]+

thanks!
0
 
CCongdonCommented:
Glad I put the second one in then! :) Not sure why it wouldn't work, but sometimes RegEx implementations differ just enough between platforms that things that defintely work in one place don't always work in another.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now