Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ MUSHclient ➜ Lua ➜ Capture word out of a line and save as a variable.

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

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Blixel   (80 posts)  Bio
Date Sat 01 Jan 2022 05:16 PM (UTC)

Amended on Sat 01 Jan 2022 05:17 PM (UTC) by Blixel

Message
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.
Top

Posted by Fiendish   USA  (2,533 posts)  Bio   Global Moderator
Date Reply #1 on Sat 01 Jan 2022 08:55 PM (UTC)

Amended on Sat 01 Jan 2022 09:03 PM (UTC) by Fiendish

Message
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.

https://github.com/fiendish/aardwolfclientpackage
Top

Posted by Nick Gammon   Australia  (23,101 posts)  Bio   Forum Administrator
Date Reply #2 on Sat 01 Jan 2022 09:02 PM (UTC)

Amended on Sat 01 Jan 2022 09:05 PM (UTC) by Nick Gammon

Message
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. :)

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Blixel   (80 posts)  Bio
Date Reply #3 on Sat 01 Jan 2022 09:26 PM (UTC)
Message
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.
Top

Posted by Blixel   (80 posts)  Bio
Date Reply #4 on Sat 01 Jan 2022 09:31 PM (UTC)
Message
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.
Top

Posted by Nick Gammon   Australia  (23,101 posts)  Bio   Forum Administrator
Date Reply #5 on Sat 01 Jan 2022 10:39 PM (UTC)
Message
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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


12,339 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.