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.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ General ➜ Regular Expression, Match previous word

Regular Expression, Match previous word

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


Posted by Joseisme   (11 posts)  Bio
Date Wed 07 Nov 2012 04:42 AM (UTC)
Message
I am trying to figure out a way to match any one word before a known word.

In these examples I am trying to match the word before 'has' so I can pass it on to other triggers so my client knows what has just died.

I am matching these.
Quote:

A dirty rat has been defeated.

or

An ancient red spirit has been defeated.


For these examples I only want the words 'rat' or 'spirit'. Not 'dirty rat' or 'ancient red spirit'. The exact number of adjectives before the main word is unknown but the following word 'has' is constant.

I have tried this but I only get 'rat has' at %3.
Quote:

^(A|a|an|An|the|The) (.*?) (.*? has) been defeated\.$


How can I get this to work so it matches the word just before 'has'?

(Using Mushclient version 4.73)
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #1 on Wed 07 Nov 2012 05:51 AM (UTC)

Amended on Wed 07 Nov 2012 05:55 AM (UTC) by Nick Gammon

Message
This, maybe?


<triggers>
  <trigger
   custom_colour="2"
   enabled="y"
   match="^(A|a|an|An|the|The) (.*) ((\w+) has) been defeated\.$"
   regexp="y"
   send_to="2"
   sequence="100"
  >
  <send>
%%1 = %1
%%2 = %2
%%3 = %3
%%4 = %4
</send>
  </trigger>
</triggers>


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


Output from your test data:


A dirty rat has been defeated.
%1 = A
%2 = dirty
%3 = rat has
%4 = rat

An ancient red spirit has been defeated.
%1 = An
%2 = ancient red
%3 = spirit has
%4 = spirit


Nested wildcards saved the day. :)

- Nick Gammon

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

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #2 on Wed 07 Nov 2012 05:54 AM (UTC)
Message
Or reduce the number of wildcards:


<triggers>
  <trigger
   custom_colour="2"
   enabled="y"
   match="^(?:A|a|an|An|the|The) .* (\w+) has been defeated\.$"
   regexp="y"
   send_to="2"
   sequence="100"
  >
  <send>
%%1 = %1
</send>
  </trigger>
</triggers>


Output:


A dirty rat has been defeated.
%1 = rat

An ancient red spirit has been defeated.
%1 = spirit

- Nick Gammon

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

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #3 on Wed 07 Nov 2012 05:56 AM (UTC)
Message
Template:regexp Regular expressions
  • Regular expressions (as used in triggers and aliases) are documented on the Regular expression tips forum page.
  • Also see how Lua string matching patterns work, as documented on the Lua string.find page.

- Nick Gammon

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

Posted by Joseisme   (11 posts)  Bio
Date Reply #4 on Fri 09 Nov 2012 12:04 PM (UTC)
Message
As always thanks for the reply, that worked like a charm. You also taught me some very useful tricks which I plan to apply to a few other triggers.
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.


15,440 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.