1

首先,如果使用正则表达式甚至是这里的最佳解决方案,我想提出一个意见,我对这个领域相当陌生,正则表达式是我发现的第一件事,而且它似乎有点容易使用,直到我需要长时间一行中的文本部分大声笑。我正在为正则表达式使用 vb.net 环境。

基本上,我在这里采取这条线:

21:24:55 "READ/WRITE: ['PASS',false,'27880739',[40,[459.313,2434.11,0.00221252]],[["ItemFlashlight","ItemWatch","ItemMap","ItemKnife","ItemEtool","ItemGPS","ItemHatchet","ItemCompass","ItemMatchbox","M9SD","ItemFlashlightRed","NVGoggles","Binocular_Vector","ItemToolbox","M4A1_AIM_SD_camo"],["ItemPainkiller","ItemMorphine","ItemSodaPepsi","FoodSteakCooked",["30Rnd_556x45_StanagSD",29],"30Rnd_556x45_StanagSD","30Rnd_556x45_StanagSD","30Rnd_556x45_StanagSD","30Rnd_556x45_StanagSD","30Rnd_556x45_StanagSD",["15Rnd_9x19_M9SD",12],["15Rnd_9x19_M9SD",10],"15Rnd_9x19_M9SD","15Rnd_9x19_M9SD","ItemBandage"]],["DZ_Backpack_EP1",[["BAF_AS50_TWS"],[1]],[["FoodSteakCooked","ItemPainkiller","ItemMorphine","ItemSodaCoke","5Rnd_127x99_as50","ItemBloodbag"],[2,1,1,2,4,1]]],[316,517,517],Sniper1_DZ,0.94]"

使用以下正则表达式:

\[\[([\w|_|\""|,]*)\],\[([\w|_|\""|,|\[|\]]*)\]\],

尝试获取以下内容:

[["ItemFlashlight","ItemWatch","ItemMap","ItemKnife","ItemEtool","ItemGPS","ItemHatchet","ItemCompass","ItemMatchbox","M9SD","ItemFlashlightRed","NVGoggles","Binocular_Vector","ItemToolbox","M4A1_AIM_SD_camo"],["ItemPainkiller","ItemMorphine","ItemSodaPepsi","FoodSteakCooked",["30Rnd_556x45_StanagSD",29],"30Rnd_556x45_StanagSD","30Rnd_556x45_StanagSD","30Rnd_556x45_StanagSD","30Rnd_556x45_StanagSD","30Rnd_556x45_StanagSD",["15Rnd_9x19_M9SD",12],["15Rnd_9x19_M9SD",10],"15Rnd_9x19_M9SD","15Rnd_9x19_M9SD","ItemBandage"]]

但是,要么我的正则表达式有缺陷,要么我的 vb.net 代码有缺陷。它只显示以下数据:

[["ItemFlashlight","ItemWatch","ItemMap","ItemKnife","ItemEtool","ItemGPS","ItemHatchet","ItemCompass","ItemMatchbox","M9SD","ItemFlashlightRed","NVGoggles","Binocular_Vector","ItemToolbox","M4A1_AIM_SD_camo"],["ItemPainkiller","ItemMorphine","ItemSodaPepsi",

如果您需要查看它,我的 vb.net 代码是:

ListView1.Clear()
    Call initList(Me.ListView1)
    My.Computer.FileSystem.CurrentDirectory = My.Settings.cfgPath
    My.Computer.FileSystem.CopyFile("arma2oaserver.RPT", "tempRPT.txt")
    Dim ScriptLine As String = ""
    Dim path As String = My.Computer.FileSystem.CurrentDirectory & "\tempRPT.txt"
    Dim lines As String() = IO.File.ReadAllLines(path, System.Text.Encoding.Default)
    Dim que = New Queue(Of String)(lines)
    ProgressBar1.Maximum = lines.Count + 1
    ProgressBar1.Value = 0
    Do While que.Count > 0
        ScriptLine = que.Dequeue()
        ScriptLine = LCase(ScriptLine)
        If InStr(ScriptLine, "login attempt:") Then
            Dim rtime As Match = Regex.Match(ScriptLine, ("(\d{1,2}:\d{2}:\d{2})"))
            Dim nam As Match = Regex.Match(ScriptLine, "\""([^)]*)\""")
            Dim name As String = nam.ToString.Replace("""", "")
            Dim next_line As String = que.Peek      'Read next line temporarily                'This is where it would move to next line temporarily to read from it
            next_line = LCase(next_line)
            If InStr(next_line, "read/write:") > 0 Then 'Or InStr(next_line, "update: [b") > 0 Then 'And InStr(next_line, "setmarkerposlocal.sqf") < 1 Then
                Dim coords As Match = Regex.Match(next_line, "\[(\d+)\,\[(-?\d+)\.\d+\,(-?\d+)\.\d+,([\d|.|-]+)\]\]")
                Dim inv As Match = Regex.Match(next_line, "\[\[([\w|_|\""|,]*)\],\[([\w|_|\""|,|\[|\]]*)\]\],") '\[\[([\w|_|\""|,]*)\],\[([\w|_|\""|,|\[|\]]*)\]\],
                '\[\[([\w|_|\""|,]*)\],\[([\w|_|\""|,|\[|\]]*)\]\]:\[([\w|_|\""|,|\[|\]]*)\]\:
                Dim back As Match = Regex.Match(next_line, "\""([\w|_]+)\"",\[\[([\w|_|\""|,]*)\],\[([\d|,]*)\]\],\[\[([\w|_|\""|,]*)\],\[([\d|,]*)\]\]")
                Dim held As Match = Regex.Match(next_line, "\[\""([\w|_|\""|,]+)\""\,\d+\]")
                With Me.ListView1
                    .Items.Add(name.ToString)
                    With .Items(.Items.Count - 1).SubItems
                        .Add(rtime.ToString)
                        .Add(coords.ToString)
                        .Add(inv.ToString)
                        .Add(back.ToString)
                        .Add(held.ToString)
                    End With
                End With
            End If
        End If
        ProgressBar1.Value += 1
    Loop
    My.Computer.FileSystem.DeleteFile("tempRPT.txt")
    ProgressBar1.Value = 0

奇怪的是,当我在 Expresso 中测试我的正则表达式时,它得到了完整、正确的匹配。所以我不知道我做错了什么。

4

3 回答 3

1

我不确定你的正则表达式有什么问题,但是这个的第一个匹配似乎工作正常:

\[\[.*?\]\]

希望这可以帮助。

-编辑-

问题不在于正则表达式,而在于 ListView 正在截断字符串的显示。看这里

于 2012-08-20T20:09:35.993 回答
0

试试这个正则表达式:\Q[[\E(?:(?!\Q[[\E).)+]]

http://regex101.com/r/zP1aC5

如果您需要反向引用,请使用\Q[[\E((?:(?!\Q[[\E).)+)]]

于 2012-08-20T20:23:48.347 回答
0

也许您应该指定是使用单行还是多行输入文本。根据您的输入文本格式,尝试使用:

Dim variableName as Match = Regex.Match("input", "pattern", RegexOptions.SingleLine);

或者

Dim variableName as Match = Regex.Match("input", "pattern", RegexOptions.Multiline);
于 2012-08-20T20:27:57.450 回答