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 help

Regular Expression help

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


Posted by Korrian   (1 post)  Bio
Date Sat 27 Oct 2001 09:57 PM (UTC)
Message
I'm attempting to combine two triggers using conditionals to make scripting the response easier.

The input is one of the following:

([ Admin whispers, "add 50 to stat hp for bubba" to you. ]

-or-

([ Admin whispers, "set stat hp to 50 for bubba" to you. ]

My expression needs to wildcard add/set, 50/hp, hp/50, and bubba. It works okay as is, except to get the data I have to go up to %8 with some of them empty if set is the command. How can I limit it to %1 - %4 when set is used?

^\([ (.+) whispers, "(add|set)(?(?<=add) (\d+) to stat (hp) for (.+)|(?: stat) (hp) to (\d+) for (.+))" to you. ]

Thanks to anyone who can help. :)
Top

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #1 on Sun 28 Oct 2001 08:45 PM (UTC)
Message
You can't really limit it to 4 wildcards, and in any event you must admit that this regular expression looks a bit complicated! :)

A simpler solution would be to keep your two triggers, and have two "helper" script functions, that rearrange the wildcard order, like this:



sub DoMessageA (strTriggerName, strTriggerLine, arrWildCards)

DoMessage arrWildCards (1), arrWildCards (2), arrWildCards (3)

end sub

sub DoMessageB (strTriggerName, strTriggerLine, arrWildCards)

DoMessage arrWildCards (1), arrWildCards (3), arrWildCards (2)

end sub

sub DoMessage (strAddSet, strHp, strPlayer)

world.note "I got: " & strAddSet & strHp & " HP " & for & strPlayer

' ------ do processing here --------

end sub



The first trigger calls DoMessageA and the second trigger calls DoMessageB. These then call DoMessage which does the actual work, with the wildcards rearranged into the correct order.

Another, perhaps simpler, solution is to use a single script (that both triggers can call) that simply detects which trigger has called it, like this:



sub DoMessage (strTriggerName, strTriggerLine, arrWildCards)

dim strAddSet, strHp, strPlayer

if strTriggerName = "trigger_a" then
  strAddSet = arrWildCards (1)
  strHp     = arrWildCards (2)
  strPlayer = arrWildCards (3)
else
  strAddSet = arrWildCards (1)
  strHp     = arrWildCards (3)
  strPlayer = arrWildCards (2)
end if

world.note "I got: " & strAddSet & strHp & " HP " & for & strPlayer

' ------ do processing here --------

end sub



This example uses the fact that each trigger has a unique label, and that the label is passed down to the script routine, so you can detect which trigger called it. The "if" then tests which trigger has called it and moves the appropriate wildcards into local variables.


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


11,145 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.