Need help with Lua pattern matching

Posted by Larkin on Tue 22 Feb 2005 08:21 PM — 3 posts, 17,038 views.

#0
I'm trying to make a pattern to match variations on prompts in Achaea/Lusternia. I've got it matching and even capturing values if I have all of them displayed in my prompt (health, mana, endurance, and willpower), but I want something that matches on other prompt configurations, also. Variations would include:

100h, 100m ex-
100h, 100m, 100e ex-
100h, 100m, 100w ex-
100h, 100m, 100e, 100w ex-

I know how to match them all in a singular regular expression. Since Lua doesn't implement the full syntax, I was wondering if anyone might be able to find a way to match them with Lua's pattern matching method.

Here's what I have so far:


  _,_,health,mana,endurance,willpower,flags = string.find(prompt, "^(%d+)h, (%d+)m, (%d+)e, (%d+)w ([cexkdb@-]+)")
USA #1
I'm not sure if this is the most elegant, but you could check for a non-match (by comparing the first return value to nil) and if the first doesn't match, try the second, then the third, and so forth.
#2
Here's basically what I ended up doing. Thanks for the idea!

function OnPluginPartialLine(msg)
  _,_,health,mana,flags = string.find(msg, "^(%d+)h, (%d+)m.* ([cexkdb@]+)\-")
  if health and mana then
    Execute("acp_health " .. health)
    Execute("acp_mana " .. mana)
    _,_,endurance = string.find(msg, ", (%d+)e")
    if endurance then
      Execute("acp_endurance " .. endurance)
    end
    _,_,willpower = string.find(msg, ", (%d+)w")
    if willpower then
      Execute("acp_willpower " .. willpower)
    end
  end
  if flags then
    _,_,cloak,eq,bal,kola,deaf,blind,phase = string.find(flags, "(c?)(e?)(x?)(k?)(d?)(b?)(@?)")
    if eq == "" then
      Execute("acp_balance eq false")
    else
      Execute("acp_balance eq true")
    end
    if bal == "" then
      Execute("acp_balance bal false")
    else
      Execute("acp_balance bal true")
    end
    if cloak == "" then
      Execute("acp_undef cloak")
    else
      Execute("acp_ondef cloak")
    end
    if kola == "" then
      Execute("acp_undef kola")
    else
      Execute("acp_ondef kola")
    end
    if deaf == "" then
      Execute("acp_undef deafness")
    else
      Execute("acp_ondef deafness")
    end
    if blind == "" then
      Execute("acp_undef blindness")
    else
      Execute("acp_ondef blindness")
    end
    if phase == "" then
      Execute("acp_undef phase")
    else
      Execute("acp_ondef phase")
    end
  end
end