Showing posts with label Regular Expressions. Show all posts
Showing posts with label Regular Expressions. Show all posts

Thursday, March 4, 2010

Regular Expressions Code Template in VBScript

The template below is to be used when you have a string and you want to get date information.

varString = the original string that will be used
iItem = Number of ocurrency (1 = first , 2 = second and so on)
sDate = The date time based on a month retrieved from the original string.

Example: 12345 12MAR 25APR 3MAY (Remember: the months are in English)…

Function CheckDate(varString, iItem, sDate)

    Dim iIndex
    Dim objRegExp, objMatch, objMatches
    Dim sDay
    Dim iMonthChar
    Dim bIsDate
    Dim strMonths
    Dim strMonth
   
    bIsDate = False
    'check if the string contains a month
    Set objRegExp = CreateObject("VBScript.RegExp")
    objRegExp.Pattern = "JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC"
    objRegExp.IgnoreCase = True
    objRegExp.Global = True
   
    'get the matches if there be
    Set objMatches = objRegExp.Execute(varString)
    iIndex = 1
    For Each objMatch In objMatches
        If iIndex = iItem Then
            strMonth = objMatch.value
            iMonthChar = objMatch.FirstIndex
            Exit For
        Else
            iIndex = iIndex + 1
        End If
    Next
    
    If strMonth = "" Then
        sDate = ""
    Else
        sDay = Mid(varString, iMonthChar - 1, 2)
        If IsNumeric(sDay) Then
            sDate = GetFormattingData(sDay & strMonth, False)
            bIsDate = IsDate(sDate)
        End If
    End If
     CheckDate = bIsDate
 End Function