regexp variable extracting (?)

Posted by Doorie on Wed 07 Mar 2012 01:22 PM — 7 posts, 27,653 views.

Finland #0
Trigger:
^((.+?) attacks you\.|(.+?) wakes up and starts hitting you\.)@

I need to assign monster name to a variable, so that i can refer it later, for example targetting the right monster.

After numerous help file/forum/whatnot browsing, testing and thinking, I _still_ fail to do this.

I hope my example is clear and enough for an easy answer, and I am sane enough to understand it :P

Australia Forum Administrator #1
Well in your case the monster name will be %1 (or maybe %2 because you have a bracket inside a bracket) inside the "send" box.

Perhaps watch this?

http://www.gammon.com.au/forum/?id=9616
Amended on Thu 08 Mar 2012 04:20 AM by Nick Gammon
Finland #2

<triggers>
  <trigger
   custom_colour="16"
   enabled="y"
   group="ZZZ TEST General Moving"
   match="^((.+?) leaves (.+)\.|(.+?) arrives (.+)\.|(.+?) gallops (.+)\.|(.+?) slithers (.+)\.|(.+?) rides (.+)\.)$"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>print(" parameters:==&gt; 0:","%0","1:","%1","2:","%2","3:","%3","4:","%4","5:","%5","6:","%6","7:","%7","8:","%8","9:","%9")</send>
  </trigger>
</triggers>


This example seems to put those player (or mob) names who _leaves_, to %2 and their direction to %3.
And those who _arrive_, names to %4 and direction to %5. And so forth...

What i need is to capture player (or mob) names to some variable what i can use later, say, targetting or telling.

How do i do that? Do I have to use GetWildcards, SetVariable, GetVariable or some other funcion? And is there a better ie. shorter way to write that <send>print(".....") part?

(Video was great, i learned few tricks to be used later, thank you for that!)
Amended on Thu 08 Mar 2012 05:11 PM by Doorie
Australia Forum Administrator #3
There's another video about variables ;)

http://www.gammon.com.au/forum/?id=10863
Finland #4
But what about inside regular expressions? How do i set some named variable (like: mobname) when i have several choices in the trigger, like:

^((.+?) hits you\.|(.+?) kicks you\.)$


What i noticed from my last message was that if the first one fires (activates) it falls into parameter %1, but the second one goes into parameter %2.

I need to grab the name of a mob that automagically attacks me when i arrive at the room. If I have only one type of information, say : "Snake attacks you!" then it is easy, it's always %1, but when there are more than one automatick attack message, then the next (.+?) goes into %2, third (.+?) to %3 and so on.
Australia Forum Administrator #5
Oh, right. Well you can use named captures, and use the (?J) option to allow duplicate names.

Then the capture is called "mob" and you can extract that as shown.


<triggers>
  <trigger
   enabled="y"
   match="(?J)^((?P&lt;mob&gt;.+?) hits you\.|(?P&lt;mob&gt;.+?) kicks you\.)$"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>
mobname = "%&lt;mob&gt;"

print ("mobname = ", mobname)
</send>
  </trigger>
</triggers>


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


The regexp part looks like this without the XML escaping:


(?J)^((?P<mob>.+?) hits you\.|(?P<mob>.+?) kicks you\.)$

Finland #6
Thank You!

That did the trick.
Now I can combine many triggers via regexping and reduce the total amount of triggers.

There are so many functions and different syntaxes that sometimes I get confused. I will code on... step by step.