【问题标题】:Parsing name/value pairs containing spaces delimited by spaces解析包含由空格分隔的空格的名称/值对
【发布时间】:2017-01-05 17:54:03
【问题描述】:

我正在尝试解析 vb.net 中的文件。该文件是 Mikrotik RouterOS 上 CLI 命令的输出。 该文件如下所示,其中 \ 和行尾表示该行在下面继续

# jan/03/2017 12:46:35 by RouterOS 6.38
# software id = 3BQ2-2I1I
#
/queue simple
add dst=WAN max-limit=5M/20M name=Compartido01 priority=1/1 queue=\
    wireless-default/wireless-default target="" total-priority=1
add max-limit=512k/2M name="6206101450 - Simone Walter (2M) (P13)" parent=\
    Compartido01 target=190.211.88.1/32
add max-limit=350k/1M name=\
    "6206130537 - Figueroa Viviana del Carmen (1M) (P14)" parent=Compartido01 \
    target=190.211.88.24/32

我设法跳过了前 4 行并将它们折叠起来,使它们看起来像这样

"add dst=WAN max-limit=5M/20M name=Compartido01 priority=1/1 queue=wireless-default/wireless-default target="" total-priority=1"
"add max-limit=512k/2M name="6206101450 - Simone Walter (2M) (P13)" parent=Compartido01 target=190.211.88.1/32"
"add max-limit=350k/1M name="6206130537 - Figueroa Viviana del Carmen (1M) (P14)" parent=Compartido01 target=190.211.88.24/32"

我需要做的是提取关于这些字符串的信息,例如“name=XXXXXX”和“target=XXXXXX” 我可以使用空格作为分隔符进行拆分,但“名称”字段中可以有空格 任何人都可以给我一个提示?

【问题讨论】:

    标签: string vb.net parsing split


    【解决方案1】:

    您需要的是一个正则表达式匹配解析器...找到一个here。看看你能不能从中做出你需要的东西。

    Imports System.Text.RegularExpressions
    
    Module Parser
    
        Public Function ParseKeyValuePairs(ByVal Buffer As String) As Dictionary(Of String, String)
            Dim Result = New Dictionary(Of String, String)
    
            '---- There are 3 sub patterns contained here, seperated at the | characters
            '     The first retrieves name="value", honoring doubled inner quotes
            '     The second retrieves name=value where value can't contain spaces
            '     The third retrieves name alone, where there is no "=value" part (ie a "flag" key
            '        where simply its existance has meaning
            Dim Pattern = "(?:(?<key>[\w-]+)\s*\=\s*""(?<value>[^""]*(?:""""[^""]*)*)"") | " & _
                          "(?:(?<key>[\w-]+)\s*\=\s*(?<value>[^""\s]*)) | " & _
                          "(?:(?<key>[\w-]+)\s*)"
            Dim r = New System.Text.RegularExpressions.Regex(Pattern, RegexOptions.IgnorePatternWhitespace)
    
            '---- parse the matches
            Dim m As System.Text.RegularExpressions.MatchCollection = r.Matches(Buffer)
    
            '---- break the matches up into Key value pairs in the return dictionary
            For Each Match As System.Text.RegularExpressions.Match In m
                Result.Add(Match.Groups("key").Value, Match.Groups("value").Value)
            Next
            Return Result
        End Function
    
        Public Sub Main()
            Dim s = "Key1=Value Key2=""My Value here"" Key3=Test Key4 Key5"
            Dim r = ParseKeyValuePairs(s)
            For Each i In r
                Debug.Print(i.Key & "=" & i.Value)
            Next
        End Sub
    End Module
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-29
      • 2016-11-23
      相关资源
      最近更新 更多