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

Posted on 2005-04-13
Last Modified: 2012-05-05
Is there a way to remove all non alphabetical and non numeric chars from some string.
Question by:Carl3003
    LVL 76

    Accepted Solution

    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

    LVL 12

    Assisted Solution

    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 = true
      set matches = regex.execute(strng)
      for each match in matches
        retstr = retstr & match.value
      'to remove the underscore, you have to do another match for that...
      regex.pattern = "_" 'match all chars, including the underscore
      regex.ignorecase = true = true
      retstr = regex.replace(retstr, "")
      set regex = nothing
      regexptest = retstr
    end function

    LVL 6

    Expert Comment

    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


    LVL 12

    Expert Comment

    Nice, snavebelac... haven't used RegEx in a long time... forgot about being able to combine them... took the safe route.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
    I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now