2

以下のような5000を超えるレコードを含むOffice2007.XLSXファイルがあります(複数行のテキストを含む単一のセル)。問題:隣接するセルに、セルからのインシデントの数を入れます。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

問題は、日付の値が一貫していないことです。日、月、および時間は1桁または2桁にすることができますが、常に改行で示されます。

私のコードソリューションは、セルを取得し、改行で分割し、最後の':'から5文字すべてをトリミングして、結果を正規表現と照合することでした。その後、いくつかの基本的な集計とテキストが隣接するセルに挿入されます。

以下は、関数がどのように呼び出されるかの例です。

'calling function from another source:

thecount = CountOfDateValues(Range("a1").Value) 'get count
Range("b1").Value = thecount 'put count to adjacent cell

文字列値を取得し、正規表現に一致するカウントを返すコードはありますか?

4

2 に答える 2

4

\ nを使用して、パターン式に改行を含めることもできます。このように、テキストを配列に分割する必要はありません。

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
于 2013-10-21T09:00:08.267 に答える
0

以下は、文字列値を受け取り、正規表現との一致数を返す関数の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

URLの参照:

改行でのMSAccess2003VBA文字列分割

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

このツールにより、さまざまな日付形式に対する正規表現のテストが非常に簡単になりました。

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

ここから事前に作成した表現を使用することで、正規表現を作成する時間を大幅に節約できました。悲しいことに、そうすることで多くを学ぶことはありませんでしたが、私はこの「今すぐ行う必要がある」という要求で多くの時間を節約できたと思います。

*注:誰かがワークログのメモをタイムスタンプで開始した場合、誤検知の可能性があります。私はこれを顧客に通知しましたが、問題はありませんでした。

于 2013-03-22T20:12:17.530 に答える