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

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
0
javilmer
Asked:
javilmer
  • 3
  • 3
  • 3
  • +2
2 Solutions
 
DustinKikuchiCommented:
Set oRE = New Regexp
oRE.Pattern = "[\d_]"
oRE.Global = True
strippedString = oRE.Replace(input, "") 

Open in new window

0
 
Patrick MatthewsCommented:
Modifying DustinKikuchi's suggestion a bit:

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

Open in new window


The \D indicates a non-digit character.  (\d indicates a digit)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
DustinKikuchiCommented:
Yep, good catch there.  Just goes to show exactly why things should always be tested first!
0
 
Terry WoodsIT GuruCommented:
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
0
 
Terry WoodsIT GuruCommented:
ha, I was too slow!
0
 
javilmerAuthor Commented:
Hello,

I got an error message : "Type Regexp is not defined"
0
 
DustinKikuchiCommented:
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?
0
 
Patrick MatthewsCommented:
Is this running in VBScript?  VBA?  VB6?  VB.Net?  Something else?

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

http://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

0
 
javilmerAuthor Commented:
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
0
 
javilmerAuthor Commented:
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
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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