removing non-alphabetical and non-numeric chars from a string?

Is there a way to remove all non alphabetical and non numeric chars from some string.
thanks
Carl3003Asked:
Who is Participating?
 
David LeeConnect With a Mentor Commented:
Hi Carl3003,

Well, here's one way.  This assumes that the initial string is called strInput.

    For x = 1 To Len(strInput)
        Select Case Asc(Mid(strInput, x, 1))
            Case 48 To 57, 65 To 90, 97 To 122
                strOutput = strOutput & Mid(strInput, x, 1)
        End Select
    Next


Cheers!
0
 
GoofyDawgConnect With a Mentor Commented:
I think this'll work, but you probably test it.

function stripNonAlphaNumericChars(strng)
  dim regex, match, matches
  set regex = new regexp
  regex.pattern = "\w" 'match all chars, including the underscore
  regex.ignorecase = true
  regex.global = true
  set matches = regex.execute(strng)
  for each match in matches
    retstr = retstr & match.value
  next
 
  'to remove the underscore, you have to do another match for that...
  regex.pattern = "_" 'match all chars, including the underscore
  regex.ignorecase = true
  regex.global = true
  retstr = regex.replace(retstr, "")
  set regex = nothing
  regexptest = retstr
end function

GoofyDawg
0
 
snavebelacCommented:
Goofydawg just about has it.  RegEx has a replace function and you can combine the expressions to do it all in one hit

      Function cleanTerm(strString)
            Dim objRegExp
            Set objRegExp = New RegExp
            objRegExp.Pattern = "[/w_]"
            objRegExp.IgnoreCase = True
            objRegExp.Global = True
            cleanterm = ObjRegExp.Replace(strString, "")
            Set objRegExp = Nothing
      End Function

That shoudl do the trick - I just modified a function I use a lot myself

HTH

C
0
 
GoofyDawgCommented:
Nice, snavebelac... haven't used RegEx in a long time... forgot about being able to combine them... took the safe route.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.