Variables in Regular Expressions

Posted by Goldenfire on Tue 23 Mar 2004 03:20 AM — 8 posts, 29,401 views.

#0
(I use MUSHCLient 3.45, just so you know :)

I currently have a variable (wf_people), of the form "foo|bar|bah|baz"

I then tried to make a trigger "^.*(?:@wfpeople).*$"

If I did the sustituion by hand (i.e. put "foo|bar|bah|baz" in the trigger) the trigger fires correctly. If I put it with @wf_people, however, it does not. I have "Enabled', "Ignore case", "Keep Evaluating", "Regular Expression", and "Expand variables" checked.

Am I just missing something stupid, or is it trying to treat the "|" in the variable as literals, or something else entirely?

(edited for stupid typos...Doh!)
Amended on Tue 23 Mar 2004 03:22 AM by Goldenfire
Australia Forum Administrator #1
The problem here is that MUSHclient tries to be smart, and assumes that if you have a special character (eg. "(") inside your variable, that it needs to be "escaped" by putting a \ in front of it, when inserting it into a regular expression.

The only real workaround here is to assemble the trigger by hand. That is, whenever you change the variable (eg. using an alias) reassemble the trigger.

There is a recent example of doing exactly that, as I had the same problem recently making a "gag" plugin. See how it was done here:

http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=3958
Australia Forum Administrator #2
See this part in particular:


Sub MakeTrigger
 
'
'  remember gag list in variable for plugin state save
'
  SetVariable "gags", ArrayExport ("gags", "|")
'
'  enable/disable trigger depending on whether we have gagged players
'
  EnableTrigger "gags", ArraySize ("gags") <> 0
'
'  change the trigger match text - replace !! by the gag list
'
  If SetTriggerOption ("gags", "match", _
     Replace (GetVariable ("regexp"), "!!", _
     ArrayExportKeys ("gags", "|"))) = eBadRegularExpression Then
    ColourNote "white", "red", _
      "Could not set trigger - check regular expression syntax"
    ColourNote "white", "red", "Regular expression is currently: " _
      & GetVariable ("regexp")
  End If

End Sub


This code keeps the "original" regular expression in a variable, and replaces the !! part of it with the variable contents.

#3
Since I was already generating the pipe-delimited list from a perlscript, it was relatively trivial to add the SetTriggerOption line :)

Have you considered, potentially, either:
a) making variables-in-regular-expression perl like (thereby requiring something like perls qw() function)...admittedly, this would break lots of things
b) adding something that's more-or-less the _opposite_ of qw(). Thereby informing any regexps that the variable is in to _not_ escape metachars.

Either of those would be nice, and would make the trigger much more readable :)
Australia Forum Administrator #4
It's a nice idea - I'll put it on the list of suggestions.
#5
Just as a note, because I hate when I post mis-information, the perl function is quotemeta() not qw

qw// does something entirely diferrent, even if the command is much shorter :)
Australia Forum Administrator #6
Version 3.48, now released, lets you do this:

@!people

This tells MUSHclient to not escape the meta-characters.
#7
sweeeeeeeeet :)

thanks muchly!