如何在vb中对字符串进行排序

时间:2014-03-07 11:37:50

标签: vb.net dictionary sorted

我遇到了这个问题。我正在使用Visual Studio Express 2013(是VB.NET吗?),这是我想要执行的任务: 我有一个.txt文件格式如下(更多行):

b2 a9 9c b4 d2 d3 52 02 da d2 e2 a2 c2 34 b2 a4 25 1c cb 52
00 00 00 00 00 00 00 00 00 00 04 c6 a2 10 a2 aa 5a 96 12 35 00 00 00 00 00 00 00 00 00 00 04 ae a5 9b 53 6c 15 56 56 d2 a1 54 55 b4 a6 ba a8 aa a6 b9 a8 44 94 69 5e d1 17 6a 56 9a 0b a7 29 49
69 d2 14 11 a1 78 41 d0 a4 54 41 51 1c 94 c1 24 a8 2a 71 14 50 14 04 b5 45 31 00 00 00 00 00 00 00 00 00 00

它们是HEX值,我需要找到重复的模式。该模式可以是2,3,4或5个字节长,但目前只能使用固定大小(即只有2个字节模式)。 我想用(Hex pattern,Repetition)填充SortedDictionary 我试过SortedDictionary和Dictionary,同样的问题。 我已经测试了10行或左右的F11调试,它运行正常。 但是,如果我使用120行文本文件(与我想要做的相比较小)运行应用程序,它就会挂起。字典中的最大元素数量是256 * 256,是不是太多了?然后用3,4或5个字节?这不仅仅是时间问题,调试器会引发异常,因为该过程不会在60秒内结束。 是否有更聪明的方式来做我想做的事情?

    Dim BytesList As New SortedDictionary(Of String, Integer)
    Dim line As String = ""
    Dim CurrentString As String
    Dim ByteLen As Byte

    For ByteLen = 2 To 2 'to do: ideally repeat for ByteLen = 2,3,4,5
        Using sr As StreamReader = New StreamReader(path & LOGFILENAME,False)
            Do
                line = line & sr.ReadLine()
                line = line.Replace(Chr(32), "") 'remove unwanted chars from line
                line = line.Replace(Chr(10), "")
                line = line.Replace(Chr(13), "")
                While (line.Length > ByteLen * 2)
                    CurrentString = line.Substring(0, ByteLen * 2)
                    line = line.Substring(2, line.Length - 2)
                    Try
                        BytesList.Add(CurrentString, 1)     'insert found address
                    Catch
                        BytesList(CurrentString) = BytesList(CurrentString) + 1 ' if string is already present, increment value
                    End Try
                End While
            Loop Until line Is Nothing
        End Using
    Next ByteLen
End Sub

提前感谢所有帮助过的人!

1 个答案:

答案 0 :(得分:0)

如果你打算找到重复的单个字节(十六进制数字对),例如00 00 00 00,那么这样的东西就可以了:

Dim pattern As String = "(?<bh>\d\d )(\k<bh>)+"
Dim rx As New Regex(pattern)
Dim match = rx.Match("00 00 00 00 00 00 00 00 00 00 04 c6 a2 10 a2 aa 5a 96 12 35 00 00 00 00 00 00 00 00 00 00 04 ae a5 9b 53 6c 15 56 56 d2 a1 54 55 b4 a6 ba a8 aa a6 b9 a8 44 94 69 5e d1 17 6a 56 9a 0b a7 29 49 ")
While match.Success
    Debug.WriteLine("'{0}' found at position {1}", match.Value, match.Index)
    match = match.NextMatch()
End While

结果

'00 00 00 00 00 00 00 00 00 00 ' found at 0
'00 00 00 00 00 00 00 00 00 00 ' found at 60
'56 56 ' found at 111

这仅适用于单行(您的示例输入的第二行),但您可以轻松扩展它以处理文件中的每一行。

相关问题