How to use named capture value in triggers

Posted by Zhenzh on Tue 16 Jul 2019 07:26 AM — 2 posts, 12,183 views.

China #0
I find there's a way to name value captured from trigger as mentioned in

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.

Regexp: tell (?<who>.+?) (?'what'.+)
Match : tell Nick hi there
Wildcard '1' = 'Nick'
Wildcard '2' = 'hi there'
Wildcard 'who' = 'Nick'
Wildcard 'what' = 'hi there'

The problem is how can I use such named capture in my script.

I used to use script like SetVariable(hp, "%1") to get the first value captured. But it can not work when I tried to use SetVariable(hp, "%hpvalue") to indicate the capture value named by 'hpvalue'.
Australia Forum Administrator #1
The help for triggers tells you how to do that. See:

http://www.gammon.com.au/scripts/doc.php?dialog=IDD_EDIT_TRIGGER

Quote:

For named wildcards you can use %<name> (for example, %<mob> for a wildcard with the name "mob"). Named wildcards are produced by using a regular expression and a group like: (?P<mob>.+?)


Also, if you use a script function (not send-to-script) then the wildcards are provided as the 3rd function parameter:


function myTriggerHandler (name, line, wildcards)
  
  print ("Wildcard 'who' is:", wildcards.who)

end -- myTriggerHandler