Register to post in forums, or Log in to your existing account
 

 Related 
Contents
Triggers
  #ACTION
  #ALARM
  #CONDITION
  #EVENT
  #FIRE
  #MXPTRIG
  #NOINPUT
  #ONINPUT
  #RAISEEVENT
  #REGEX
  #SENDSB
  #SETPROMPT
  #TEMP
  #TRIGGER
  %alarm
  %state
  Create/Modify Settings
  Introduction to Triggers
  Multistate triggers
  Trigger Tester
  Trigger Types
  Triggers
  Triggers
Triggers
  Pattern Matching
  Auto Login
  Trigger Types
  Trigger Tester
  Multistate triggers
Related Links:
  %grep
  #TRIGGER
  Introduction to Triggers
  Expressions
  Editing Triggers
  Triggers
  Overriding the Mapper
  0: For the Novice
Pattern Matching [[cmud_Pattern_Match]] 
Pattern Matching

Patterns can contain several special character for wild-card matching:

* match any number (even none) of characters or white space
? match a single character
%d match any number of digits (0-9)
%n match a number that starts with a + or - sign
%w match any number of alpha characters (a-z) (a word)
%a match any number of alphanumeric characters (a-z,0-9)
%s match any amount of white space (spaces, tabs)
%x match any amount of non-white space
%y match any amount of non-white space (same as %x but matches start and end of line)
%p match any punctuation
%q match any punctuation (same as %p but matches start and end of line)
%t match a direction command
%e match ESC character for ansi patterns
[range] match any amount of characters listed in range
^ force pattern to match starting at the beginning of the line
$ force pattern to match ending at the end of the line
(pattern) save the matched pattern in a parameter %1 though %99
~ quote the next character to prevent it to be interpreted as a wild card, required to match special characters
~~ match a quote character verbatim
{val1|val2|val3|...} match any of the specified strings a preference controls the use of wildcards here
@variable match any of the specified strings or keys works with string lists and record variables
{^string} do not match the specified string
&nn matches exactly nn characters (fixed width pattern)
&VarName assigns the matched string to the given variable (see below for more info)
%/regex/% matches the given Regular Expression (Added in v2.0)
%%function() runs any CMUD function (Added in v2.06)

To save any part of the pattern to the %1..%99 parameters, enclose the part of the parameter in parenthesis.

In specifying a range, you can list specific characters such as [abc] or you can use a range [a-c].

To use a wild card character in the pattern string itself, precede the special character with the ~ quote character. For example, the pattern ~[test~] will match the string [test] rather than being interpreted as a range wild-card pattern. Note that the quote character can be changed in the Preferences section.

To match a blank line, use the $ pattern by itself. To match multiple lines include $ in the middle of the pattern to match the line boundary.

You can also include variables in your pattern, and the name of the variable will be replaced with its value before the pattern match is performed.

Using &VarName

The &VarName syntax deserves a bit more explanation. It is used to store a matched pattern into a variable directly. For example, if you have a trigger:

#TRIGGER {You get &Gold coins}

and the MUD displays

You get 1000 coins

Then the value 1000 is automatically put into the @Gold variable with no further action on your part. By default, the &VarName uses the * wildcard. To specify a different wildcard, insert the wildcard just after the & character. In the above example, when the MUD says

You get many coins

the @Gold variable would then contain the string "many". To restrict the trigger to just match numbers, change the trigger to:

#TRIGGER {You get &%dGold coins}

Now the "You get many coins" will not match the trigger. You can use any of the wildcard specifiers, and can also use the square brackets [] to define your own wildcard range.

Sometimes there is alphabetic text in the pattern after the string you want to capture. To delimit this from the variable name, place the variable name in {} brackets. For example, to capture

You have 1000gp

you would use a trigger:

#TRIGGER {You have &%d{Gold}gp}

which will only match digits and will properly set the @Gold variable to 1000.

The &VarName syntax also works with database records and record variables. If you have a record variable @char and want to set the Gold property, you would use a trigger of:

#TRIGGER {You get &char.Gold coins}

To set the Gold field of record 7 in the database, you would do

#TRIGGER {You get &7.Gold coins}

To set the Gold field of the current database record, leave out the record number all together with the trigger

#TRIGGER {You get &.Gold coins}

This syntax along with the database provides a powerful way to capture information from the MUD.

NOTE: The patterns matched by the &VarName syntax still count towards the %1..%99 variables and can still be accessed in that manner. So, in the example:

#TRIGGER {Hp: &HpVar Mana: &ManaVar} {#SHOW %1 %2}

it will automatically set the @HpVar and @ManaVar variables, and the %1 and %2 parameters will also still be set just as if you had put parenthesis around the &VarName. This is important when you mix &VarName and () in the same trigger. Always count the &VarName as if it also has a parenthesis around it.

 User comments 
Fang Xianfu: Fri Jul 20, 2007 8:04 pm    

%n also matches comma-spaced numbers like 296,682,672,682 and it strips the commas when it matches.
Fang Xianfu: Sun Jul 29, 2007 8:37 pm    

%q doesn't actually match punctuation.

#trig {test: (something%q)} {#say %1}
#say test: something.

will echo "something" and not "something." like you'd expect. This is because %q actually matches a word-boundary, the gap between a word character (a-zA-Z0-9) and something that's not a word character. %q is the same as the regex \b.

See this thread about \b.
Fang Xianfu: Thu Aug 02, 2007 4:38 am    

The * wildcard includes protection against trigger abuse. See these two posts [1][2] for a discussion and for information on disabling it. Use the protection-disabled wildcard with extreme care! Don't let it near the #send, #sendraw or #exec commands!
Viewer Comments [3 - Post your comments]

Jump to:  

© 2009 Zugg Software. Hosted by Wolfpaw.net