Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ Need help with Lua pattern matching

Need help with Lua pattern matching

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Larkin   (278 posts)  Bio
Date Tue 22 Feb 2005 08:21 PM (UTC)
Message
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@-]+)")
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #1 on Tue 22 Feb 2005 08:59 PM (UTC)
Message
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.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Larkin   (278 posts)  Bio
Date Reply #2 on Thu 24 Feb 2005 03:52 PM (UTC)
Message
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
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


16,272 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.