如何剖析和解析 lua 中的字符串?
How to dissect and parse a string in lua?
我正在尝试在 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.match
:
Message=" /kill playername "
command, arg = Message:match("%s*/(.-)%s+(.*)%s*$")
如果你想让它在未来对更多命令更灵活,我建议你把 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。一旦您知道要看什么,它们就很容易阅读。
我建议使用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)
我正在尝试在 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.match
:
Message=" /kill playername "
command, arg = Message:match("%s*/(.-)%s+(.*)%s*$")
如果你想让它在未来对更多命令更灵活,我建议你把 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。一旦您知道要看什么,它们就很容易阅读。
我建议使用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)