【问题标题】:How to dissect and parse a string in lua?如何剖析和解析lua中的字符串?
【发布时间】:2020-01-08 16:15:56
【问题描述】:

我正在尝试在 Roblox 中创建命令参数。例如,/kill playername。问题是我不知道如何从字符串/kill playername 中解析玩家名。这段代码是这样的:

game:GetService("Players").PlayerAdded:Connect(function(Player)
    Player.Chatted:Connect(function(Message)
        if string.sub(1, #Message) == "/kill " then
            --this means the string starts with /kill and is expecting an argument. 
            --How can I parse this argument from the string
        end
    end)
end)

编辑:我想添加/setdata <Playername> <DataToChange eg. money> <Value> 示例命令:

/setdata MyRobloxUsername Money 10000

我正在尝试使用类似的东西来这样做

local Command, Playername, DataToChange, Value = string.match(???)

我只需要将字符串中的值转换为变量。我可以自己弄清楚如何使用变量来更改数据。只是如何从字符串中获取值。我该怎么做?

我不接受这个答案,因为我需要进一步的帮助。一旦我得到这个帮助,我会重新接受它。我的下一个请求是类似的,但使用 3 个参数而不是 1 个。我需要帮助,因为 string:Match() 对我来说非常反直觉

【问题讨论】:

    标签: string lua roblox


    【解决方案1】:

    使用string.match:

    Message=" /kill playername  "
    command, arg = Message:match("%s*/(.-)%s+(.*)%s*$")
    

    【讨论】:

    • 这不是一个可扩展的解决方案。 @JulianTiemann 我建议您使用 string split 等效项,它将单独的单词放在表格中。
    【解决方案2】:

    如果您希望将来对更多命令更加灵活,我建议您将 lhf 和 BotOfWar 的建议结合起来。

    local function executeCommandInMessage(message)
        -- do a quick regex of the message to see if it is formatted as a command
        -- all we care about is the command, any arguments are optional.
        local command, arguments = string.match(message, "^/(%w+)[%s]?([%w%s]+)$")
    
        if command ~= nil then
            -- we've found a command, parse the arguments into groups of non-space characters
            -- then store each word in the parts array
            local parts = {}
            for w in arguments:gmatch("%S+") do
                table.insert(parts, w)
            end
    
            -- handle each command individually
            if command == "kill" then
                local player = parts[1]
                print(string.format("Killing %s", player))
    
            elseif command == "setdata" then
                local player = parts[1]
                local value = parts[2]
                local amount = parts[3]
                print(string.format("Setting %s on %s to %s", value, player, amount))
    
            -- add any further commands to the list..
            -- elseif command == "" then
            end
        end
    end
    
    
    -- listen for any message submitted by players
    game:GetService("Players").PlayerAdded:Connect(function(Player)
        Player.Chatted:Connect(function(msg)
            -- check for any commands
            executeCommandInMessage(msg)
        end)
    end)
    

    以后如果你需要更好的正则表达式来解析消息,建议你看看Lua pattern matching是怎么做的。一旦你知道要看什么,它们就很容易阅读。

    【讨论】:

    • 此解决方案对没有参数的命令有问题。 '/nukeAll' 之类的东西将无法正确解析。
    【解决方案3】:

    我建议使用string.split 方法拆分字符串以获得段,然后检查第一个值是否是您想要的。

    game:GetService("Players").PlayerAdded:Connect(function(Player)
        Player.Chatted:Connect(function(Message)
            local segments = Message:split(" ")
            if((#segments >= 1) and (segments[1] == "/kill")) then
                -- The rest of the arguments can be accessed like this:
                local args = {unpack(segments, 2)} -- Gets every argument after the first value,
                -- which is the command.
            end
        end)
    end)
    

    【讨论】:

    • 我会投赞成票,但我因“粗鲁或辱骂”而失去了 30 个声誉
    猜你喜欢
    • 2012-01-31
    • 1970-01-01
    • 2016-07-02
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    • 2014-10-17
    • 2020-09-04
    • 2014-01-01
    相关资源
    最近更新 更多