|
- Sub test()
- Dim r%, i%
- Dim arr, brr
- Dim reg1 As New RegExp
- Dim reg2 As New RegExp
- Dim reg3 As New RegExp
- Dim flg1 As Boolean
- Dim flg2 As Boolean
- Dim mypath$, myname$
- With reg1
- .Global = False
- .Pattern = "交易数量为([\d\.]+)的商品:(\d+)价格为:([\d\.]+)"
- End With
- With reg2
- .Global = False
- .Pattern = "零售手输会员卡卡号:([\d]+)"
- End With
- With reg3
- .Global = False
- .Pattern = "(\d{4}-\d{2}-\d{2})\s+\d{2}:\d{2}:\d{2}\s+\d{3}\s+付款方式\s+储值卡:([\d\.]+)"
- End With
-
- mypath = ThisWorkbook.Path & ""
- myname = "log20180902.txt"
- If Dir(mypath & myname) = "" Then
- MsgBox mypath & myname & "不存在!"
- Exit Sub
- End If
- ss = ReadUTF(mypath & myname)
- arr = Split(ss, vbCrLf)
- ReDim brr(1 To UBound(arr), 1 To 3)
- ReDim crr(1 To UBound(arr), 1 To 3)
- m = 0
- n = 0
- flg1 = False
- For i = 0 To UBound(arr)
- ss = Trim(arr(i))
- If InStr(ss, "开始新的交易") <> 0 Then
- flg1 = True
- ElseIf InStr(ss, "交易保存成功") <> 0 Then
- flg1 = False
- End If
- If flg1 Then
- Set mh = reg1.Execute(ss)
- If mh.Count > 0 Then
- m = m + 1
- brr(m, 1) = mh(0).SubMatches(1)
- brr(m, 2) = mh(0).SubMatches(0)
- brr(m, 3) = mh(0).SubMatches(2)
- End If
- End If
- Next
-
- For i = UBound(arr) To 0 Step -1
- ss = Trim(arr(i))
- If InStr(ss, "交易保存成功") <> 0 Then
- flg1 = True
- ElseIf InStr(ss, "开始新的交易") <> 0 Then
- flg1 = False
- End If
- If flg1 Then
- Set mh = reg3.Execute(ss)
- If mh.Count > 0 Then
- n = n + 1
- crr(n, 1) = mh(0).SubMatches(0)
- crr(n, 3) = mh(0).SubMatches(1)
- flg2 = True
- End If
- If flg2 Then
- Set mh = reg2.Execute(ss)
- If mh.Count > 0 Then
- crr(n, 2) = mh(0).SubMatches(0)
- flg2 = False
- End If
- End If
- End If
- Next
-
- With Worksheets("sheet1")
- .UsedRange.Offset(1, 0).ClearContents
- .Columns(1).NumberFormatLocal = "@"
- If m > 0 Then
- .Range("a2").Resize(m, UBound(brr, 2)) = brr
- End If
- r = .Cells(.Rows.Count, 1).End(xlUp).Row
- .Range("a1:c" & r).Borders.LineStyle = xlContinuous
- End With
- With Worksheets("sheet2")
- .UsedRange.Offset(1, 0).ClearContents
- .Columns(2).NumberFormatLocal = "@"
- If n > 0 Then
- .Range("a2").Resize(n, UBound(crr, 2)) = crr
- End If
- r = .Cells(.Rows.Count, 1).End(xlUp).Row
- .Range("a1:c" & r).Borders.LineStyle = xlContinuous
- End With
- End Sub
- Function ReadUTF(ByVal FileName As String) As String
- With CreateObject("ADODB.Stream")
- .Type = 2 '读取文本文件
- .Mode = 3 '读写
- .Open
- .LoadFromFile FileName
- .Charset = "UTF-8" '设定编码
- .Position = 2
- ReadUTF = .ReadText
- .Close
- End With
- End Function
复制代码 |
|