下面是一个函数的 VBA 代码,它接受一个字符串值并将匹配计数返回给正则表达式。我希望它对某人有用。
Function CountOfDateValues(thetext)
Dim data() As String
Dim yourInput As String
yourInput = thetext
Dim TheSplitter As String
TheSplitter = Chr(10) 'the character that represents a line break
data = Split(yourInput, TheSplitter ) ' creates an array of strings for each line in the cell
Dim re
Set re = CreateObject("VBscript.regexp")
'regular expression that matches ##/##/#### ##:##:## ##
re.Pattern = "(?=\d)^(?:(?!(?:10\D(?:0?[5-9]|1[0-4])\D(?:1582))|(?:0?9\D(?:0?[3-9]|1[0-3])\D(?:1752)))((?:0?[13578]|1[02])|(?:0?[469]|11)(?!\/31)(?!-31)(?!\.31)|(?:0?2(?=.?(?:(?:29.(?!000[04]|(?:(?:1[^0-6]|[2468][^048]|[3579][^26])00))(?:(?:(?:\d\d)(?:[02468][048]|[13579][26])(?!\x20BC))|(?:00(?:42|3[0369]|2[147]|1[258]|09)\x20BC))))))|(?:0?2(?=.(?:(?:\d\D)|(?:[01]\d)|(?:2[0-8])))))([-.\/])(0?[1-9]|[12]\d|3[01])\2(?!0000)((?=(?:00(?:4[0-5]|[0-3]?\d)\x20BC)|(?:\d{4}(?!\x20BC)))\d{4}(?:\x20BC)?)(?:$|(?=\x20\d)\x20))?((?:(?:0?[1-9]|1[012])(?::[0-5]\d){0,2}(?:\x20[aApP][mM]))|(?:[01]\d|2[0-3])(?::[0-5]\d){1,2})?$"
re.Global = True
Dim t As String
Dim theCount As Integer
theCount = 0
For i = LBound(data) To UBound(data) 'from first item in array to last item in array
For Each Match In re.Execute(Left(data(i), InStrRev(data(i), ":") + 5))
'from start of string to 5 characters past the last ':' of string
theCount = theCount + 1
Next
Next
CountOfDateValues = theCount
End Function
参考网址:
MS Access 2003 VBA 字符串在换行符处拆分
http://sourceforge.net/projects/regexbuilder/files/regexbuilder/1.4.0/
该工具使针对各种日期格式测试我的正则表达式变得非常容易。
http://regexlib.com/DisplayPatterns.aspx?cattabindex=4&categoryid=5&p=2
通过使用这里的预制正则表达式,我能够节省大量时间来制作正则表达式。遗憾的是,这样做并没有学到很多东西,但我相信我在“我们现在需要完成”的请求上节省了很多时间。
*注意:如果有人使用时间戳开始他们的工作日志记录,则会出现误报窗口,我向客户说明了这一点,他们对此很好。