Brent Hacker
Brent Hacker

Reputation: 394

regex not matching correctly

First of all, I would like an opinion if using regex is even the best solution here, I'm fairly new to this area and regex is the first thing I found and it seemed somewhat easy to use, until I need to grab a long section of text out of a line lol. I'm using a vb.net environment for regex.

Basically, I'm taking this line here:

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]"

Using the following regex:

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

To try and get the following:

[["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"]]

However either my regex is flawed, or my vb.net code is. It only displays the following data:

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

My vb.net code in case you need to peek at it is:

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

The odd thing is, when I test my regex in Expresso it gets the full, correct match. So I don't know what I'm doing wrong.

Upvotes: 1

Views: 893

Answers (3)

Nathan Andrew Mullenax
Nathan Andrew Mullenax

Reputation: 824

I'm not sure what's wrong with the regex you have, but the first match off of this one seems to work fine:

\[\[.*?\]\]

Hope this helps.

-EDIT-

The problem isn't the regex, it's that ListView is truncating the display of the string. See here

Upvotes: 1

Nikola Malešević
Nikola Malešević

Reputation: 1858

Perhaps you should specify whether you are working with single line or multi line input text. Depending on your input text format, try with:

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

or

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

Upvotes: 0

Firas Dib
Firas Dib

Reputation: 2621

Try this regular expression instead: \Q[[\E(?:(?!\Q[[\E).)+]]

http://regex101.com/r/zP1aC5

If you need a backref, use \Q[[\E((?:(?!\Q[[\E).)+)]]

Upvotes: 0

Related Questions