Didn’t manage to make it a single pattern, but this works.
local pattern = "%f[%S]:%w+"
local pattern2 = "^:%w+"
local str = ':one :two:three' -- Expect :one and :two
for cmd in string.gmatch(str,pattern) do
print(cmd)
end
print(string.match(str,pattern2))
-- :dd
-- :Dd
Is there a reason why it needs to be one pattern and with no if statements? The only way I would see how to do it without either of those is to always prepend the string you’re searching with a space, i.e.
which still technically breaks your “one pattern” rule (and the first pattern is essentially “if you don’t start with whitespace, prepend with a space”).
Edit: added the end of input magic character to the pattern
I have converted your REGEX into a way to understand what it’s doing, for others to read.
^.*%s:([^%s%p]+)$
Symbol
Sentence
^
Locate at the start of a string…
.*
a set of random characters (can be none)…
%s
with a white space…
:
followed by a semicolon…
([^%s%p]+)
with any set of characters that doesn’t contain any white space or punctuation marks…
$
which must take up the whole string.
Where it’s ending at is the %s, because if your string only contains :test, it will fail because there is no white space characters anywhere in the text.
This is impossible to fix in REGEX form, so instead you can prepend your string with a white space at the beginning.