Problem with Regexp

Posted by Halig on Tue 29 Dec 2015 11:23 PM — 7 posts, 22,676 views.

Portugal #0
Hello to you all. It's been a while. I have a new problem that i can't seem to fix.


<triggers>
  <trigger
   enabled="y"
   match="([^.]+)(\s+)([^.]+) Dried fruit rations"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>dried = "%1"

first = string.match(dried, "((%d+))")

SetVariable ("dried", first)

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


The MUD output is:

(71) (Assorted) Dried fruit rations

I'm trying to save to a variable the number of Dried fruit rations that i have on the inventory.

That works, but if i send to the MUD "drop dried" it gives the following error:

[string "Trigger: "]:5: bad argument #2 to 'SetVariable' (string expected, got nil)
stack traceback:
[C]: in function 'SetVariable'
[string "Trigger: "]:5: in main chunk

How can i fix this?

Thank you.
Australia Forum Administrator #1
Quote:

That works, but if i send to the MUD "drop dried" it gives the following error:


If you send "drop dried" what is the MUD output?
Portugal #2
Hi Nick,

The MUD output is:

You release (Assorted) Dried fruit rations from your grasp.
(Assorted) Dried fruit rations hits the ground.
Portugal #3
I've figured it out.

Made two triggers.


<triggers>
  <trigger
   enabled="y"
   group="Inventory"
   lines_to_match="2"
   match="You are carrying:"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>EnableTrigger ( "inv", true )</send>
  </trigger>
</triggers>


And that one, enables the second one:


<triggers>
  <trigger
   group="Inventory"
   match="^([^.]+)(.*)([^.]+) Dried fruit rations"
   name="inv"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>number = "%1"

first = string.match(number, "((%d+))")

SetVariable ("dried", first)

EnableTrigger ( "inv", false )</send>
  </trigger>
</triggers>


This way all is working like i wanted to.

Thank you.
Amended on Wed 30 Dec 2015 08:44 PM by Nick Gammon
Australia Forum Administrator #4
I was about to suggest that:


 (Assorted) Dried fruit rations hits the ground.


That has more words after "Dried fruit rations" so you could have put a "$" at the end of the regexp to make it match that text with nothing after it.
Portugal #5
Hi Nick.

At least i learned something.
I've changed the all thing to the $ that you said, at least i don't have too many triggers :).

Thank you.
Australia Forum Administrator #6
The other thing you can do, since you want to match a number (or it won't work) is to make that explicit. Change:


([^.]+)


at the start of the regexp to:


([0-9]+)


Now that won't match on "You release" so the trigger doesn't fire.