2017-01-05 7 views
0

私は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 

の下に続けて、彼らはこの

"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つの最初の行をスキップし、それらを崩壊するために管理意味し、このようになります

私がする必要があるのは、 "name = XXXXXX"や "target = XXXXX"のような文字列の情報を抽出することです。 区切り文字としてスペースを使用して分割できますが、 "name"フィールドにはスペースを入れることができます 私にヒントをください ?

答えて

1

必要なものは、RegExマッチパーサーです。見つかったものは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 
関連する問題