Trouble with triggers.

Posted by FishOnSpeed on Fri 28 Mar 2008 06:16 PM — 7 posts, 31,684 views.

USA #0
Okay. I have two triggers that are very similar. I don't have them doing anything of any value yet ... but I still need them to work.

Heres the problem.

You get * silver coins and * gold coins from the corpse of *.
You get * gold coins from the corpse of *.

So when I only get gold coins it uses the second trigger but when I get gold and silver it still uses the second trigger and tries to set "12 silver coins and" as the first variable. But I want it to use the first trigger I listed.

Is there any way for me to make the client use the one I want when I want it to?
USA #1
Using the generic * will grab anything, numeric or otherwise into the variable. The simple fix is just to set the silver and gold one to a lower sequence than the gold only one. The lower the sequence value, the earlier the trigger engine will evaluate matching. The more accurate fix is to use regular expressions to grab the data.


"^You get (\d+) silver coins and (\d+) gold coins from the corpse of (.*)\.$"
"^You get (\d+) gold coins from the corpse of (.*)\.$"
USA #2
Alright. But what exactly are the (/d+) and the (.*)\ things? What are they doing? And do I use the same %1 ... %2 .. blah blah things to use numbers later in the trigger?
Australia Forum Administrator #3
Take a look at http://www.mushclient.com/regexp

Also, inside the trigger, once you have checked "regular expression" and click on the "..." box to edit the match text, there is a new button (marked "->") which lets you insert regular expression fields easily.

To get started, take your original trigger, which is:


You get * gold coins from the corpse of *.


Then click on the button "Convert to regular expression". This changes it to:


^You get (.*?) gold coins from the corpse of (.*?)\.$


You can see most is the same, except the wildcards are now: (.*?)

In a regular expression, something inside brackets is a "capture" (what we call a wildcard). So, the first capture becomes %1, the second %2, and so on, just like in a normal trigger.

In fact, internally MUSHclient converts all non-regular expression triggers to regular expressions using exactly that method, so it behaves exactly the same.

The dot character means "match anything", unless escaped by a backslash. So the final dot in your original text needs to be literally a dot, so it is there as: \.

The ^ means "anchor to start of the line", and the $ means "anchor to the end of the line". This stops it matching something like:


John says, "You get 20 gold coins from the corpse of the kobold."


Having got this far, let's see what Shaun Biggs suggested. Taking your converted regular expression:


^You get (.*?) gold coins from the corpse of (.*?)\.$


He suggested making it more specific. The regular expression documentation shows that you can match on digits only by using \d.

Also, the "*" character means "zero or more", but we really expect at least one digit, so "+" is used instead, which means "one or more".

Thus the simple change to:


^You get (\d+) gold coins from the corpse of (.*?)\.$


This makes sure that inside wildcard %1 will only be digits - and therefore the text "12 silver coins and" will not match it.

Another way of doing the same thing is with a character class, like this:


^You get ([0-9]+) gold coins from the corpse of (.*?)\.$


Here you are explicitly saying you only want 0-9 (the square brackets let you designate sets of exact characters or ranges).

You could also limit the number of digits, like this:


^You get ([0-9]{1,4}) gold coins from the corpse of (.*?)\.$


Now we are saying we only want between 1 and 4 digits (so a 5-digit number would not match).
Amended on Fri 28 Mar 2008 10:56 PM by Nick Gammon
USA #4
Here's an odd question, since we are making the regex more complicated... is the gold display separated by commas? having a script pull out the commas after capturing the variable might be useful in that case.

^You get (\d{1,3}(?:\,\d{3})*) gold coins from the corpse of (.*?)\.$

Also "\d" is shorthand for "[0-9]" just to clear up any confusion for people not used to regex
USA #5
No there aren't any commas in it. I posted it exactly as it appears on my screen from the mud. Thanks for the help with that.

But now I'm having another problem.

The triggers work right now but if I check the "omit from output" box the trigger no longer sends the information I want it to.

I don't have it doing anything at the moment really. I just want it to omit the long line sent by the mud and replace it with a Note. But when I omit the text the Note no longer pops up. It only shows the Note when I don't gag the text.
Australia Forum Administrator #6
See http://mushclient.com/faq point 51 (recently added).