User Tools

Site Tools


mud:regexps

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

mud:regexps [2019/08/08 14:55] (current)
admin created
Line 1: Line 1:
 +The majority of matching in MUX is done with wildcard ("​globbing"​) patterns. There is a second type of matching, using regular expressions,​ that is available in certain circumstances.
 +
 +For attributes that are $-commands or ^-listen-patterns,​ setting that attribute "​regexp"​ (with ''​@set <​object>/<​attribute>​=regexp''​) causes patterns to be matched using regular expressions rather than globbing.
 +
 +In a regular expression match, the substring of the string which matched the regexp pattern is %0; %1 through %9 are the substrings of the string which matched parenthesized expressions within the regexp pattern.
 +
 +Regular expressions are extremely useful when you want to enforce a data type. For example, if you have a command where you want a player to enter a string and a number ('​+setnum <​player>​=<​number>',​ for example), you might do it like this:
 +
 +<​code>​
 +  &DO_NUM Command Object=$+setnum (.+)=([0-9]*):​ @va me=Data: %1 = %2
 +  @set Command Object/​DO_NUM = regexp
 +</​code>​
 +
 +Then, ''​+setnum cookies=30''​ would set VA to "Data: cookies = 30".
 +
 +This eliminates your having to check to see if the player entered a number, since the regular expression matches only numbers. ​ Furthermore,​ the '​+'​ guarantees that there needs to be at least  one character there, so a player can't enter '​+setnum cookies=' ​ or '​+setnum =10' or similarly malformed input.
 +
 +Also note that regexps are case-sensitive,​ but wildcard globbing is not.  Some regular expression functions offer case-insensitive versions, some regexp can be made case-insensitive by putting (?i) at the start.
 +
 +Setting the CASE attribute flag is another way of selecting case-insensitivity.
 +
 +Related Topics: [[mud:​regexp syntax]]
 +
  
mud/regexps.txt ยท Last modified: 2019/08/08 14:55 by admin