Regular Expressions Pattern Matching using VBScript.Regexp

Guus2005

AWF VIP
Local time
Today, 08:12
Joined
Jun 26, 2007
Messages
2,642
Below you will find two functions which i've created for pattern matching using VBScript.Regexp.

At the bottom there is a sample which i use to clear the Tag property of controls which name start with txt followed by a two digit number.
Quick and easy.

More info on regular expressions: http://www.regular-expressions.info/
Code:
'Add regular expression functionality using vbscript for fast and easy pattern matching.
Public Function Regexp(strData As String, strPattern As String) As String
    
    Dim oRegexp As Object, oMatches As Object
     
    On Error GoTo Error_Here

    If Len(strData) = 0 Or Len(strPattern) = 0 Then Exit Function
     
    Set oRegexp = CreateObject("vbscript.regexp")
    
    With oRegexp
        .MultiLine = False
        .Global = False
        .IgnoreCase = True
        .Pattern = strPattern
    End With
     
    Set oMatches = oRegexp.Execute(strData)
    Regexp = oMatches(0)

Exit_Here:
    Exit Function
Error_Here:
    Regexp = ""
    Resume Exit_Here
End Function
Public Function RegexpBln(strData As String, strPattern As String) As Boolean
    RegexpBln = Len(Regexp(strData, strPattern)) > 0
End Function

'Sample:
Public Sub ClearTags(frm as form)
'Tag property will be cleared if control name matches "txt" followed by a two digit number
'More info: http://www.regular-expressions.info/

    Dim ctl As Control

    For Each ctl In frm.Controls
        If TypeOf ctl Is TextBox Then
            If RegexpBln(ctl.Name, "^txt[0-9][0-9]$") Then
                ctl.Tag = ""
            End If
        End If
    Next ctl

End Sub
Share & Enjoy!
 

Users who are viewing this thread

Back
Top Bottom