用于匹配日期的VB6正则表达式需要调整

时间:2012-03-06 05:01:33

标签: regex vb6

我正在尝试将VB6正则表达式与捕获组一起使用来解析和重新排列字符串:

Dim innfilename As String
Dim outfilename As String
innfilename = "4.6.12.Jack&DianeWedding004.jpg"
outfilename = innfilename

Dim regexB As RegExp
Dim regexBMatchCol As MatchCollection
Dim regexBMatch As Match
Set regexB = New RegExp
regexB.IgnoreCase = True
regexB.Global = True
regexB.Pattern = "^(\d{1,2})\.(\d{1,2})\.(\d{2,4})\.(.*)$"
Set regexBMatchCol = regexB.Execute(innfilename)

If regexBMatchCol.Count > 0 Then
    Set regexBMatch = regexBMatchCol(0)
    mnth = regexBMatch.SubMatches(0)
    dayy = regexBMatch.SubMatches(1)
    year = regexBMatch.SubMatches(2)
    remd = regexBMatch.SubMatches(3)
    yearInt = Val(year)
    mnthInt = Val(mnth)
    dayyInt = Val(dayy)
    If yearInt >= 70 And yearInt <= 99 Then
        year = "19" & year
    Else
        year = "20" & year
    End If
    If mnthInt >= 1 And mnthInt <= 9 Then
        mnth = "0" & mnth
    End If
    If dayyInt >= 1 And dayyInt <= 9 Then
        dayy = "0" & dayy
    End If
    outfilename = year & "." & mnth & "." & dayy & "." & remd
End If

但我的正则表达式无效,即regexBMatchCol.Count结束为零。 任何人都可以发现我的错误吗?

TIA,

仍在学习史蒂夫

1 个答案:

答案 0 :(得分:0)

如果您想使用正则表达式将日期与MM.DD.YYYY模式匹配,请使用:

regexB.Pattern = "(\d{1,2})\.(\d{1,2})\.(\d{2,4})"