Link to home
Start Free TrialLog in
Avatar of javilmer
javilmer

asked on

VBScript REGEX get rid of numbers

Hello,

I need a small VBScript Regex function that takes out of a string any character that is not a number.

Exemple :

01.45a(f689!$ should be transformed to 0145689

Thanks for help
Avatar of Seaton007
Seaton007
Flag of United States of America image

SOLUTION
Avatar of DustinKikuchi
DustinKikuchi
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yep, good catch there.  Just goes to show exactly why things should always be tested first!
I'm not familiar with VBScript (my specialty is regex's), but @DustinKikuchi's pattern seems wrong. I think it should be this:

Set oRE = New Regexp
oRE.Pattern = "\D"
oRE.Global = True
strippedString = oRE.Replace(input, "") 

Open in new window


If you accept my answer, please share points with DustinKikuchi if you made use of his code... thanks
ha, I was too slow!
Avatar of javilmer
javilmer

ASKER

Hello,

I got an error message : "Type Regexp is not defined"
The Regexp object type was introduced in VBScript v5.0 (aka quite awhile ago).  I do now notice, however, that you have categorized the question under Visual Basic programming.  If you're attempting to implement this in VB.net it won't work.

Where exactly are you attempting to use this?
Is this running in VBScript?  VBA?  VB6?  VB.Net?  Something else?

:)
If VBA or VB6, you might want to check out my article:

https://www.experts-exchange.com/Programming/Languages/Visual_Basic/A_1336-Using-Regular-Expressions-in-Visual-Basic-for-Applications-and-Visual-Basic-6.html

The RegExpReplace function from that article might be useful:

VariableB = RegExpReplace(VariableA, "\D", "")

Function RegExpReplace(LookIn As String, PatternStr As String, Optional ReplaceWith As String = "", _
    Optional ReplaceAll As Boolean = True, Optional MatchCase As Boolean = True, _
    Optional MultiLine As Boolean = False)
    
    ' Function written by Patrick G. Matthews.  You may use and distribute this code freely,
    ' as long as you properly credit and attribute authorship and the URL of where you
    ' found the code
    
    ' This function relies on the VBScript version of Regular Expressions, and thus some of
    ' the functionality available in Perl and/or .Net may not be available.  The full extent
    ' of what functionality will be available on any given computer is based on which version
    ' of the VBScript runtime is installed on that computer
    
    ' This function uses Regular Expressions to parse a string, and replace parts of the string
    ' matching the specified pattern with another string.  The optional argument ReplaceAll
    ' controls whether all instances of the matched string are replaced (True) or just the first
    ' instance (False)
    
    ' If you need to replace the Nth match, or a range of matches, then use RegExpReplaceRange
    ' instead
    
    ' By default, RegExp is case-sensitive in pattern-matching.  To keep this, omit MatchCase or
    ' set it to True
    
    ' If you use this function from Excel, you may substitute range references for all the arguments
    
    ' Normally as an object variable I would set the RegX variable to Nothing; however, in cases
    ' where a large number of calls to this function are made, making RegX a static variable that
    ' preserves its state in between calls significantly improves performance
    
    Static RegX As Object
    
    If RegX Is Nothing Then Set RegX = CreateObject("VBScript.RegExp")
    With RegX
        .Pattern = PatternStr
        .Global = ReplaceAll
        .IgnoreCase = Not MatchCase
        .MultiLine = MultiLine
    End With
    
    RegExpReplace = RegX.Replace(LookIn, ReplaceWith)
    
End Function

Open in new window

HEllo,

I use visual studio 2008 in an SQL server. Actually I do this to move from T-SQL function to assemblies (dll), for performance reasons
OK, I just needed to rewrite as :

        Dim strippedString As String
        Dim oRE As Object
        oRE = CreateObject("VBScript.RegExp")
        oRE.Pattern = "\D"
        oRE.Global = True
        strippedString = oRE.Replace(S, "")

and it works perfectly
thanks a lot