I'm attempting to match Sword
and 2
in the following string.
You receive loot [Sword]x2.
This is where I've made it so far. Sword matches fine and is saved in the item variable. qty, however, always returns 'No qty' regardless of the input string.
local item, qty = msg:match('%[(.+)%]x?(%d?)') or 'No item', 'No qty'
The problem is not your pattern, it's the way the multiple assignment together with or
works. What you have is actually (note the bold parens):
local item, qty =
(
msg:match('%[(.+)%]x?(%d?)') or 'No item'
)
, 'No qty'
So, qty
will always be assigned 'No qty'
. I don't think this problem can be solved in a single statement. You'll have to do something like this:
local item, qty = msg:match('%[(.+)%]x?(%d?)')
item = item or 'No item'
qty = qty or 'No qty'
or
local item, qty = msg:match('%[(.+)%]x?(%d?)')
item, qty = item or 'No item', qty or 'No qty'
Concerning the pattern, you might want to use %[(.+)%]x?(%d*)
, i.e. *
instead of ?
for quantities of 10 or more.