Regex 正则表达式不正确匹配

Regex 正则表达式不正确匹配,regex,vb.net,match,Regex,Vb.net,Match,首先,如果使用正则表达式是这里最好的解决方案,我想听听你的意见。我对这个领域相当陌生,正则表达式是我发现的第一件事,它似乎很容易使用,直到我需要从一行lol中抓取一长段文本。我正在使用一个vb.net环境来处理正则表达式 基本上,我在这里用这句话: 21:24:55 "READ/WRITE: ['PASS',false,'27880739',[40,[459.313,2434.11,0.00221252]],[["ItemFlashlight","ItemWatch","ItemMap","It

首先,如果使用正则表达式是这里最好的解决方案,我想听听你的意见。我对这个领域相当陌生,正则表达式是我发现的第一件事,它似乎很容易使用,直到我需要从一行lol中抓取一长段文本。我正在使用一个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
ListView1.Clear()
调用initList(Me.ListView1)
My.Computer.FileSystem.CurrentDirectory=My.Settings.cfgPath
My.Computer.FileSystem.CopyFile(“arma2oaserver.RPT”、“tempRPT.txt”)
将脚本行变暗为字符串=“”
Dim路径为String=My.Computer.FileSystem.CurrentDirectory&“\tempRPT.txt”
将行的大小调整为String()=IO.File.ReadAllLines(路径,System.Text.Encoding.Default)
Dim que=新队列(字符串)(行)
ProgressBar1.最大值=行数.计数+1
ProgressBar1.Value=0
当que.Count>0时执行此操作
ScriptLine=que.Dequeue()
ScriptLine=LCase(ScriptLine)
如果InStr(脚本行,“登录尝试:”),则
Dim rtime As Match=Regex.Match(脚本行,(“(\d{1,2}:\d{2}:\d{2})”)
Dim nam As Match=Regex.Match(脚本行“\”([^)]*)\”)
Dim名称为String=nam.ToString.Replace(“”,“”)
将下一行变暗为String=que。Peek“临时读取下一行”这是它将临时移动到下一行以从中读取的位置
下一行=LCase(下一行)
如果InStr(下一行,“读/写:”)>0 Then'或InStr(下一行,“更新:[b”)>0 Then'和InStr(下一行,“setmarkerposlocal.sqf”)<1则
将坐标调整为Match=Regex.Match(下一行,\[(\d+)\,\[(\d+).\d+\,((\d+).\d+,([\d-)\]\])
Dim inv As Match=Regex.Match(下一行“\[\[([\w||||||\”,]*)\[([\w|||||\”,]*)\[\[([\w|||||\”,]*)\],\[([\w| |||u124;\”,]*)\,[],
“\[\[([\w | | | |,]*)\],\[([\w | | | | | | | | | | | | | | | | | | | | | |]*)\]:\[([\w | | | | | |
暗显为Match=Regex.Match(下一行“\”([\w|||+)\”、\[\[([\w||||\”、]*)\]、\[([\d|,]*)\]、\[\[([\w| ||||\”、]*)\]、\[([\d|、]*)\]))
Dim保持为Match=Regex.Match(下一行“\[\”([\w | | | | | | | | | | | | | | | | | | | |
和我一起。ListView1
.Items.Add(name.ToString)
带有.Items(.Items.Count-1).SubItems
.Add(rtime.ToString)
.Add(coords.ToString)
.Add(inv.ToString)
.Add(back.ToString)
.Add(hold.ToString)
以
以
如果结束
如果结束
ProgressBar1.值+=1
环
My.Computer.FileSystem.DeleteFile(“tempRPT.txt”)
ProgressBar1.Value=0

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

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

\[\[.*?\]\]
希望这有帮助

-编辑-


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

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

\[\[.*?\]\]
希望这有帮助

-编辑-


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

请尝试使用此正则表达式:
\Q[\E(?:(?!\Q[\E)。+]


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

尝试使用以下正则表达式:
\Q[\E(?(?!\Q[\E.)+]


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

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

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


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

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


这对我来说似乎不起作用,它以“itemsoadapepsi”结尾,就像我的一样。正则表达式有字符限制吗?@BrentHacker:不,它没有字符限制。另外,澄清一下,你的表达式匹配得很好。只是你在第一个backref中保存了第一部分,在第二个backref中保存了另一部分。然而,它是一个写得非常糟糕的表达式。只是想一想:可能是ListView不正确吗不显示字符串的其余部分?是的--。有259个字符的限制。它似乎不适合我。它以“itemsodapesi”结尾,就像我的一样。正则表达式有字符限制吗?@BrentHacker:不,它没有字符限制。另外,澄清一下,你的表达式匹配得很好。只是你在第一个backref中保存了第一部分,在第二个backref中保存了另一部分。然而,它是一个写得非常糟糕的表达式。只是想一想:可能是ListView不正确吗t显示字符串的其余部分?是的--。有259个字符的限制。