How to dissect and parse a string in lua?

試著忘記壹切 提交于 2020-06-23 09:55:09

问题


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)

Edit: I want to add /setdata <Playername> <DataToChange eg. money> <Value> Example command:

/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?

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


回答1:


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.




回答2:


Use string.match:

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



回答3:


I suggest splitting the string with the string.split method to get the segments, then check if the first value is what you want.

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)


来源:https://stackoverflow.com/questions/57810931/how-to-dissect-and-parse-a-string-in-lua

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!