rex:match pattern works in immediate window but not in a send to script trigger?

Posted by Eupher on Tue 10 Aug 2010 11:12 PM — 4 posts, 18,566 views.

#0
I'm new to this and probably doing something stupid... but here goes...

I have a trigger in my world file that calls rex:match in the send to script section. The pattern is not matching there, but if I use the same pattern (and test text) in the immediate window then it matches.

Trigger pattern: ^God tells you: "do (?<order>.+)"$
regular expression is checked
send to script is selected

send:
re = rex.new([[^(t|te|tel|tell)\s+(\S+)\s+do\s+(.+)$]])
s,e,t = re:match("%<order>")
print("%<order>")
print(s,e,t)

When I test the trigger, I get the following output:

God tells you: "do tell me do kill"
tell me do kill
nil nil nil

However, if I put the following script into the immediate window:

re = rex.new([[^(t|te|tel|tell)\s+(\S+)\s+do\s+(.+)$]])
s,e,t = re:match("tell me do kill")
print(s,e,t)

I get the following output:

1 15 table: 03B09530

Why is it matching in one place and not the other?

Thanks :)
Australia Forum Administrator #1
It would help if you simply pasted the XML from the trigger rather than explaining it piece by piece.

Template:copying
For advice on how to copy aliases, timers or triggers from within MUSHclient, and paste them into a forum message, please see Copying XML.


Your problem is that inside the Send box, MUSHclient pre-processes backslash sequences, so that \s won't be sent to the rex.new.


re = rex.new([[^(t|te|tel|tell)\s+(\S+)\s+do\s+(.+)$]])


You need to defeat that by doubling the backslashes, this worked for me:


<triggers>
  <trigger
   enabled="y"
   match="^God tells you: &quot;do (?&lt;order&gt;.+)&quot;$"
   regexp="y"
   send_to="12"
   sequence="110"
  >
  <send>

re = rex.new([[^(t|te|tel|tell)\\s+(\\S+)\\s+do\\s+(.+)$]])
s,e,t = re:match("%&lt;order&gt;")
print("%&lt;order&gt;")
print(s,e,t)

</send>
  </trigger>
</triggers>



Template:pasting
For advice on how to copy the above, and paste it into MUSHclient, please see Pasting XML.


If this is confusing, use a script file and put the name of a function in the "Script" box of the trigger, and have the function in your script file. Then you don't need to double the backslashes.

For example:


<triggers>
  <trigger
   enabled="y"
   match="^God tells you: &quot;do (?&lt;order&gt;.+)&quot;$"
   regexp="y"
   script="mytrigger"
   sequence="110"
  >
  </trigger>
</triggers>


And in your script file:



function mytrigger (name, line, wildcards)
  re = rex.new([[^(t|te|tel|tell)\s+(\S+)\s+do\s+(.+)$]])
  s,e,t = re:match(wildcards.order)
  print(wildcards.order)
  print(s,e,t)

  -- debugging  
  require "tprint"
  tprint (t)
end -- function mytrigger


Output:


God tells you: "do tell me do kill"

tell me do kill
1 15 table: 03917CC0
1="tell"
2="me"
3="kill"

Amended on Tue 10 Aug 2010 11:32 PM by Nick Gammon
Australia Forum Administrator #2
And it would then be more efficient to only "compile" the regexp once:




-- do this when script loaded
re = rex.new([[^(t|te|tel|tell)\s+(\S+)\s+do\s+(.+)$]])

function mytrigger (name, line, wildcards)
  s,e,t = re:match(wildcards.order)
  print(wildcards.order)
  print(s,e,t)

  -- debugging  
  require "tprint"
  tprint (t)
end -- function mytrigger

#3
Thanks for the help. I'll be sure to post the xml next time. :)