VBA Regular Expression to Match Date

后端 未结 2 1756
栀梦
栀梦 2020-12-22 12:16

I\'m new to Regular Expressions and am having difficulty getting patterns that I find online to work in VBScript/VBA. This one is supposed to return a date found in a string

2条回答
  •  悲哀的现实
    2020-12-22 13:04

    Why not use RegEx to get the portion of the string that appears to be the date and use the IsDate Function to validate it?

    Function FormatOutput(s)
        Dim re, match
        Set re = CreateObject("vbscript.regexp")
        re.Pattern = "[\d]+[\/-][\d]+[\/-][\d]+"
        re.Global = True
    
        For Each match In re.Execute(s)
            if IsDate(match.value) then
                FormatOutput = CDate(match.value)
                Exit For
            end if
        Next
        Set re = Nothing
    
    End Function
    

    The RegEx could be cleared up a bit, but it works for your current example.

提交回复
热议问题