Aliases Containing Regex Patterns?

Posted by Nazdravanix on Tue 13 Jan 2015 01:20 AM — 3 posts, 14,335 views.

New Zealand #0
Mushclient v 4.96 win7 Lua Scripting

So I have been hacking away at this in a rather primitive manner when it occurred to me I may be creating a bit of an xy problem. Hopefully someone can help me out.

On the mud I play (Discworld) I have a spell that when cast on an item takes you to a location "remembered" by that item. Currently I have 3 aliases for this as I have three different groups of items that need to be manipulated in different ways before the spell is cast (removed from containers etc).

What I want to do is have one master alias that depending on what the matched pattern is behaves in three different ways. I have three group of items. One is nicknamed internally within the mud as three letter codes (eg "rts" or "stl"), the second set is likewise nicknamed but with 3 digit numbers (eg "234" "456" "098"), and the third are just items that I type the full name of (eg "dagger scabbard" "hauberk" "cane").

Each set needs a different set of actions so I tried the following test alias.


<aliases>
  <alias
   name="TestPort"
   match="^test (.*)$"
   enabled="y"
   expand_variables="y"
   group="Testing"
   regexp="y"
   send_to="12"
   ignore_case="y"
   keep_evaluating="y"
   sequence="100"
  >
  <send>if "%1" == "(^[a-z]{3}$)" then
  print ("paper")

elseif "%1" == "(^[0-9]{3}$)"
  print ("rock")

else
  print ("scissors")

end -- if

-- test multi function alias</send>
  </alias>
</aliases>



'test abc' should print "paper"
'test 123' should print "rock"
'test anything else' should print "scissors"

I have tried changing my regex patterns, I have tried multiple brackets and quote locations still can't seem to get it to work. Ive only been trying out this more advanced stuff for a couple of days but I still am a bit down on myself for not being able to nut this out. Can someone put me out of my misery.
Australia Forum Administrator #1

if "%1" == "(^[a-z]{3}$)" then


An "if" statement does not evaluate regular expressions.

You want something like:


if string.match ("%1", "(^[a-z]{3}$)") then


Even then, Lua regexps do not work with {3}, so you really want something more like:



if string.match ("%1", "^%%a%%a%%a$") then


I doubled the % symbols because of the way they are processed inside a "send to script" box.

http://www.gammon.com.au/scripts/doc.php?lua=string.find
New Zealand #2
Thanks Nick. Might look into some Lua tutorials so I can avoid simple mistakes like this again.