LUA错误“模式太复杂”-桌面模拟器

我有一个字符串,我试图使用 LUA 模式匹配从中提取一个特定的部分。我将其保存为正则表达式,您可以看到here,以及提取我想要的确切部分(绿色捕获组)的字符串和正则表达式语法。

我有一个字符串,我试图使用 LUA 模式匹配从中提取一个特定的部分。我将其保存为正则表达式,您可以看到here,以及提取我想要的确切部分(绿色捕获组)的字符串和正则表达式语法。

result = {string.match(description, "Weapons.-\n(.*)\n\n")}

但是它错误地说“模式到复杂”。奇怪的是,我试图解决这个问题,因为我认为我在转换中犯了一个错误,如果我删除最后一个\n它确实起作用,但它也捕获了能力部分,这是不可取的。我认为我的语法是正确的,因为当我删除\n时,它们也从正则表达式中删除了什么

我有很多不同的方式,我得到一些奇怪的结果,所以我开始认为这是 LUA 本身的错误。

我想指出的一个额外的事情可能会有所帮助,那就是我在 Tabletop Simulator 中这样做,我相信它使用 Moonsharp(这是一个 LUA 解释器)。

Thanks,

1

我有很多不同的方式,我得到一些奇怪的结果,所以我开始认为这是 LUA 本身的错误。

这似乎是底层MoonSharp implementation的错误。正如已经在评论中指出的那样,您的模式在使用 PUC Lua 5.3 实现的大型输入字符串上运行得很好:

> description = "[-]Weapons" .. ("."):rep(1e6) .. "\n" .. ("."):rep(1234567) .. "\n\n[-]More Stuff" .. ("."):rep(1e7)
> #string.match(description, "Weapons.-\n(.*)\n\n")
1234567

考虑到 MoonSharp 的不可靠的模式实现(代码似乎端口 Lua 实现,但我认为他们忘记了增量matchdepth当函数返回时),我会实现这个匹配没有模式通过循环在行或使用找到模式项目find(不使用模式,虽然)。

以下函数对固定模式"Weapons.-\n(.-)\n\n"执行此操作。请注意,为了防止模式匹配,如何将所有find调用的最后一个参数设置为true

local function extract_weapons(description)
    local _, end_weapons = description:find("Weapons", 1, true)
    if not end_weapons then return end
    local _, end_newline = description:find("\n", end_weapons + 1, true)
    if not end_newline then return end
    local start_newlines = description:find("\n\n", end_newline + 1, true)
    if not start_newlines then return end
    return description:sub(end_newline + 1, start_newlines - 1)
end
0

您可以使用

result = s:match("Weapons.-\n(.-)\n\n")
See theonline Lua demo.Details: Weapons-a word

.--任何零个或多个字符,尽可能少

\n-换行符

(.-)-第 1 组:任何零个或多个字符,尽可能少

\n\n-两个换行符。

0

所以,对于任何其他可能遇到这个问题的人,这里是修复和为什么会发生。

正如其他人所说,这确实是由于 Tabletop Simulator 的 LUA 解释器中的一个错误。TTS 不使用本地 LUA,而是使用名为 MoonSharp v2.0 的解释器。这个版本有这个错误,似乎当你的正则表达式(pattern)匹配一个长字符串时,它是错误的。只是想强调最后一句话-它不是你正在解析的字符串,因为有这个限制

解决方法是放入一个解决方法。我首先将较大的字符串 (参见上面的 regex 链接的字符串示例) 拆分为单独的行,并与它们创建一个数组 (LUA 中的一个表)。然后,我通过循环遍历数组中的每个项目并连接它们来重建原始字符串。在循环中,我将有一个 if 语句,它查找字符串“Abilities”,一旦匹配,它将退出原工作减号。

代码片段在这里,所以你可以看到它的要点:

--this first line gets the data you see in the regex I listed above
local weaponSection = {string.match(description, "Weapons.-\n(.*)\n")}
-- Because Moonsharp regex is bugged we have to split the entire weapon section string  into subcomponents then rebuild it
    local temptable = {}
    local rebuiltWeaponSection = ""
    -- split the larger string into line by line, then insert into array - this bring "abilities" section across which we don't want and can't exclude due to bug explained above
        for weapon in string.gmatch(weaponSection[1], ".-\n") do
            table.insert(temptable, weapon)
        end
        -- now loop through the array and concat each line to a new string
        for _, weapon in ipairs(temptable) do
            -- this if statement looks for the abilities line and then exits loop when he sees it. this ultimately ends up rebuilding it all without the abilities section
            if string.match(weapon, "Abilities") then
                break
            else
                rebuiltWeaponSection = rebuiltWeaponSection .. weapon
            end
        end

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(985)
如何在 python中安装一个阶跃函数
上一篇
如何在 python中安装一个阶跃函数
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(12条)