模式匹配计数在Excel中(正则expression式和VBA)

我有一个Office 2007 .XLSX文件,其中包含如下所示的5000个以上的logging(具有多行文本的单个单元格)。 问题:在一个相邻的单元格上,把单元格的事件计数。 查看A1的单元格数据,可以看到3个事件:

单元格A1:

 1/15/2013 1:30:11 AM Userx Had to reboot system 1/15/2013 1:32:11 AM Userx System running finished rebooting and appears to be working 11/15/2013 12:30:11 AM Userx System hung again 

问题是date值不一致。 日,月和小时可以是单位或双位数字,但总是记在新行上。

我的代码解决scheme是取出单元格,将其拆分,在最后一个“:”后面修整5个字符,然后根据我的正则expression式评估结果。 之后,一些基本的理货和文本插入相邻的单元格。

下面是如何调用函数的一个例子。

 'calling function from another source: thecount = CountOfDateValues(Range("a1").Value) 'get count Range("b1").Value = thecount 'put count to adjacent cell 

是否有任何代码将采取string值并返回正则expression式的匹配计数?

您还可以使用\ n在模式expression式中包含换行符。 这样,你不必拆分数组中的文本:

 Private Function String_CountRegExp_Debug() 'Input of the test text Dim TestText As String TestText = "1/15/2013 1:30:11 AM Userx" & vbNewLine & _ "Had to reboot system" & vbNewLine & _ "1/15/2013 1:32:11 AM Userx" & vbNewLine & _ "System running finished rebooting and appears to be working" & vbNewLine & _ "11/15/2013 12:30:11 AM Userx" & vbNewLine & _ "System hung again" 'Input of the Pattern Dim RE_Pattern As String RE_Pattern = "(\d{1,2})\/(\d{1,2})\/(\d{4})\s(\d{1,2}):(\d{1,2}):(\d{1,2})\s([A,P]M).*\n" Debug.Print String_CountRegExp(TestText, RE_Pattern) End Function Public Function String_CountRegExp(Text As String, Pattern As String) As Long 'Count the number of Pattern matches in a string. 'Set up regular expression object Dim RE As New RegExp RE.Pattern = Pattern RE.Global = True RE.IgnoreCase = True RE.MultiLine = True 'Retrieve all matches Dim Matches As MatchCollection Set Matches = RE.Execute(Text) 'Return the corrected count of matches String_CountRegExp = Matches.Count End Function 

下面是一个函数的VBA代码,它接受一个string值并将正确expression式的匹配计数返回。 我希望这对某个人有用。

 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 

引用url:

MS Access 2003 VBAstring拆行

http://sourceforge.net/projects/regexbuilder/files/regexbuilder/1.4.0/

这个工具使得我对各种date格式的正则expression式进行testing非常容易。

http://regexlib.com/DisplayPatterns.aspx?cattabindex=4&categoryid=5&p=2

我可以节省大量的时间,通过使用从这里prerafted一个正则expression式。 可悲的是,这样做并没有学到太多东西,但是我相信我在这个“我们现在需要做的”的要求上节省了很多时间。

*注意:如果有人用时间戳开始他们的工作日志logging,有一个误报的窗口,我注意到这个给客户,他们没有问题。