1

我想要完成的是在包含以下内容的哈希集中:

MSI (c) (AC:C0) [14:23:21:685]: Back from server. Return value: 1603
MSI (c) (AC:C0) [14:23:21:685]: Decrementing counter to disable shutdown. If counter >= 0, shutdown will be denied.  Counter after decrement: -1
MSI (c) (AC:C0) [14:23:21:685]: PROPERTY CHANGE: Deleting SECONDSEQUENCE property. Its current value is '1'.
Action ended 14:23:21: ExecuteAction. Return value 3.
MSI (c) (AC:C0) [14:23:21:685]: Doing action: SetupCompleteError
Action 14:23:21: SetupCompleteError. 
Action start 14:23:21: SetupCompleteError.

找到返回值 3. 时;该行和它之前的行应该添加到第二个哈希集中。

MSI (c) (AC:C0) [14:23:21:685]: PROPERTY CHANGE: Deleting SECONDSEQUENCE property. Its current value is '1'.
Action ended 14:23:21: ExecuteAction. Return value 3.)

这可以作为正则表达式来查找该行和上面的行(据我所知),但到目前为止,我试图将这两行移动到下一个哈希集的所有操作都只移动包含返回值 3 的行。

    Dim regrv3 As New Regex("(.*)\S\s(.*)Return value 3.")

相关代码:

Dim opened As New HashSet(Of String)(File.ReadAllLines(openfile))
Dim compa As HashSet(Of String) = New HashSet(Of String)

For Each StringMatch In opened
    Dim m As Match
    m = regrv3.Match(StringMatch)
    compa.Add(m.ToString
Next

我是否遗漏了一些明显的东西,或者我是否需要某种复杂的索引来执行此操作?

4

1 回答 1

1

我还没有收到您的回复,所以我假设带有“返回值 3”的行,并且前一行应该被视为哈希集中的一个条目。此解决方案不使用正则表达式,但这应该可以实现您的目标并仍然产生哈希集​​结果。

Dim inputLines = File.ReadAllLines(filepath)
Dim matches As New HashSet(Of String)

For i = 0 To inputLines.Length - 1
  If inputLines(i).Contains("Return value 3.")
    'Okay we found what we're looking for, combine with previous line
    'Btw... there's an exception hidden here (hint: when i=0)
    Dim match = inputLines(i - 1) & inputLines(i)
    matches.Add(match)
  EndIf
Next
于 2013-09-06T21:46:48.613 回答