Regular Expression help

Posted by Korrian on Sat 27 Oct 2001 09:57 PM — 2 posts, 11,347 views.

#0
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. :)
Australia Forum Administrator #1
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.