如何在lua中解析和解析字符串? [英] How to dissect and parse a string in lua?

查看:73
本文介绍了如何在lua中解析和解析字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to make command arguments in Roblox. For example, /kill playername. The problem is I don't know how to parse the playername from the string /kill playername. This code is in something like this:

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 ;< DataToChange例如钱>< Value> 示例命令:

/setdata MyRobloxUsername Money 10000

我正在尝试使用类似的方法

I am trying to use something like this to do so

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

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

I just need to get the values from the string into variables. I can figure out how to change the data using the variables myself. Just how to get the values from the string. How can I do what I am describing?

我无法接受答案,因为我需要进一步的帮助.获得此帮助后,我将重新接受它.我的下一个请求与此类似,但是使用3个参数而不是1个参数.我需要帮助,因为 string:Match()对我来说非常直观

I unaccepted the answer because I need further help. Once I get this help I will re accept it. My next request is similar, but with 3 arguments instead of 1. I need help as string:Match() is very counter intuitive to me

推荐答案

如果你希望这在未来对更多命令更加灵活,我建议你同时接受 lhf 和 BotOfWar 的建议并结合它们.

If you want this to be more flexible to more commands in the future, I suggest you take both lhf's and BotOfWar's suggestions and combine them.

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)

将来,如果您需要更好的正则表达式来解析消息,建议您看看如何做

In the future, if you need a better regex to parse the message, I suggest you take a look at how to do Lua pattern matching. They're pretty easy to read once you know what to look at.

这篇关于如何在lua中解析和解析字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆