IMO string.split
isn’t the best way to go about this specifically because it splits things that you don’t want to split.
Instead, try using string.match(str, pattern, startpos?)
.
For example:
local message = ":kick zcr breaking rules 5, 1, 4"
local command, who, reason = string.match(message, "^:(%S+)%s+(%S+)%s+(.*)")
if command == nil then return end -- stop if the pattern wasn't matched
-- command = kick
-- who = zcr
-- reason = breaking rules 5, 1, 4
The pattern ^:(%S+)%s+(%S+)%s+(.*)
means this:
^
: beginning of the string
:
: just a colon
%S
: stands for anything that’s not whitespace (spaces etc.), the opposite of %s
(space characters)
%S+
: at least one of anything that’s not whitespace
(%S+)
: capture that as one of the return values
%s+
or just +
(space plus): at least one space
.*
: any amount of anything
and later ()
: the position of this match
Of course, the format of “colon at the start, word, word, rest of the message” won’t fit every use case.
So first look for the colon and first word, and let each command handle its arguments separately.
local message = ":kick zcr breaking rules 5, 1, 4"
local command, nextpos = string.match(message, "^:(%S+)%s+()")
-- command = kick
-- nextpos = 7
if command == nil then
-- message did not begin with a colon
-- or it began with a colon and a space
return
elseif command == "kick" then
local who, reason = string.match(message, "^(%S+)%s+(.*)", nextpos)
-- who = zcr
-- reason = breaking rules 5, 1, 4
else
-- etc etc.
end