Capture word out of a line and save as a variable.

Posted by Blixel on Sat 01 Jan 2022 05:16 PM — 6 posts, 22,326 views.

#0
In my game I have a trigger as follows:

^\>?A (.*?) appears suddenly and attacks!$


When this text appears and activates the trigger, I'm able to save the name of the monster as a variable. I'm trying to do the exact same thing where I save the name of the weapon I'm using as a variable, but for reasons I don't understand, it's not working.

Here is the text coming from the game.

You are wearing platemail and a shield and are armed with a halberd.


Here is the new trigger I'm trying to create.

^\>?You are wearing .*are armed with a (.*?)


This is being triggered, but it's not capturing and saving the name of my weapon.
Amended on Sat 01 Jan 2022 05:17 PM by Blixel
USA Global Moderator #1
Quote:
This is being triggered

Ok, good.

Quote:
but it's not capturing and saving the name of my weapon

Correct. You made the capture optional with "?". Either end your pattern with "$" or remove the "?" or both.
Amended on Sat 01 Jan 2022 09:03 PM by Fiendish
Australia Forum Administrator #2
It's useful to test out regexps at Regular Expressions 101


There are two fixes that will make it work.

1.


^\>?You are wearing .*are armed with a (.*?)$

The extra $ (assert end of line) forces the regexp to proceed to the end, thus capturing the weapon name.

2.


^\>?You are wearing .*are armed with a (.*)


(Remove the last "?"). By putting a "?" there you are making the weapon name optional, and the regular expression terminates because it doesn't need to go any further.




Or, as Fiendish just said, make both changes. :)
Amended on Sat 01 Jan 2022 09:05 PM by Nick Gammon
#3
Fiendish said:

Quote:
This is being triggered

Ok, good.


I recorded a short video clip to illustrate what was going on in response to your original message (https://www.youtube.com/watch?v=6UTv74SJno8), but it looks like your response was edited since then. Thanks. It helped.
#4
Nick Gammon said:

It's useful to test out regexps at [url=https://regex101.com/]Regular Expressions 101[/url]


Thanks - I'm using that now to try to figure out another problem I'm having were "M - werewolf, wolf" and "M - wolf, werewolf" are confusing my trigger that is trying to catch "wolf" while ignoring "werewolf".


Nick Gammon said:

There are two fixes that will make it work.

1.


^\>?You are wearing .*are armed with a (.*?)$

The extra $ (assert end of line) forces the regexp to proceed to the end, thus capturing the weapon name.

2.


^\>?You are wearing .*are armed with a (.*)


(Remove the last "?"). By putting a "?" there you are making the weapon name optional, and the regular expression terminates because it doesn't need to go any further.


Jeez, so simple but so complex. Since (.*?) worked in the other trigger, it never would have occurred to me to try (.*) this time. But sure enough, that works. To prevent capturing the period at the end of the sentence, I changed the final regex this this:

^\>?You are wearing .*are armed with a (.*).*\.$


..and that seems to do the trick.
Australia Forum Administrator #5
Hmmm.

I would be cautious with this sequence: "(.*).*".

That seems pretty ambiguous, you want to match "anything" followed by "anything". Where does the first "anything" end and the second one start?

My guess is that the first one will be greedy and thus the second one will never match anything at all. So, leave it out.


Quote:

Thanks - I'm using that now to try to figure out another problem I'm having were "M - werewolf, wolf" and "M - wolf, werewolf" are confusing my trigger that is trying to catch "wolf" while ignoring "werewolf".


Make another post, and include the full trigger:


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.