Tomasz Kulig kuligtom@poczta.onet.pl http://kuligtom.republika.pl/ Feb 16 2002 version 1.0 R.dll is the MudMaster and MudMaster 2000 library which allows you to introduce Regular expressions into your scripts. The code has been derived from work by Henry Spencer and from some pices of code written by Guy Gascoigne - Piggford. The main part of code is compatible with TinyFudge regexp. I am not native speaker so if you find any mistakes please inform me. ------------------------- Usage: /calldll R Set {^(dragon|vampire) kills (knight|warrior)} sets regular expression ()+?*[]|^$ allowed ` is escape character /calldll R iSet {^(dragon|vampire) kills (knight|warrior)} same as above but case insensitive /calldll R Match {vampire kills knight} matches string to set regular expression /calldll R Replace {`2 was killed by `1} replaces string using subsets from match (use `0 `1 ... `9) ------------------------- Read-only variables returned by all functions. P# - number of matched subexpressions P0 - whole matched expression P1 - one of matched subexpression () ... P9 - one of matched subexpression () PL, PR - left and right part of string which was not matched Pe - whole regular expression Rerror - number of error, 0 means no errors RerrorString - the reason of error Rmatch - last match result Rreplace - replaced string ------------------------- Regular Expression Syntax A regular expression is zero or more branches, separated by '|'. It matches anything that matches one of the branches. A branch is zero or more pieces, concatenated. It matches a match for the first, followed by a match for the second, etc. A piece is an atom possibly followed by '*', '+', or '?'. An atom followed by '*' matches a sequence of 0 or more matches of the atom. An atom followed by '+' matches a sequence of 1 or more matches of the atom. An atom followed by '?' matches a match of the atom, or the null string. An atom is a regular expression in parentheses (matching a match for the regular expression), a range (see below), '.' (matching any single character), '^' (matching the null string at the beginning of the input string), '$' (matching the null string at the end of the input string), a '`' followed by a single character (matching that character), or a single character with no other significance (matching that character). A range is a sequence of characters enclosed in '[]'. It normally matches any single character from the sequence. If the sequence begins with '^', it matches any single character not from the rest of the sequence. If two characters in the sequence are separated by '-', this is shorthand for the full list of ASCII characters between them (e.g. '[0-9]' matches any decimal digit). To include a literal ']' in the sequence, make it the first character (following a possible '^'). To include a literal '-', make it the first or last character. ------------------------- //////////////////////////////////////////////////////////////////////// // RegExp.h // // This code has been derived from work by Henry Spencer. // The main changes are // 1. All char variables and functions have been changed to TCHAR // counterparts // 2. Added GetFindLen() & GetReplaceString() to enable search // and replace operations. // 3. And of course, added the C++ Wrapper // // The original copyright notice follows: // // Copyright (c) 1986, 1993, 1995 by University of Toronto. // Written by Henry Spencer. Not derived from licensed software. // // Permission is granted to anyone to use this software for any // purpose on any computer system, and to redistribute it in any way, // subject to the following restrictions: // // 1. The author is not responsible for the consequences of use of // this software, no matter how awful, even if they arise // from defects in it. // // 2. The origin of this software must not be misrepresented, either // by explicit claim or by omission. // // 3. Altered versions must be plainly marked as such, and must not // be misrepresented (by explicit claim or omission) as being // the original software. // // Altered by Kuling. Escape char == '`'. 16-2-2002. // // 4. This notice must not be removed or altered. ///////////////////////////////////////////////////////////////////////////// Source code is available. --------------- Examples for newbies. How to extract money from this line? You must pay 4 gold and 5 silver coins. Example 1. /calldll R iSet {(.*) attacks you} /calldll R Match {Dragon attacks you} /if {$Rerror==0 && $Rmatch==1} {/showme {$P1} {/showme {Incorrect}} $P1 = "Dragon" Example 2. /calldll R Set { goes ([^ ]*)`.\$} /calldll R Match {Red fox goes north-east.} $P0 = " goes north-east." $P1 = "north-east" $PL = "Red fox" $PR = "" Example 3. /calldll R Set {You must pay (([0-9]+) gold[a-z ]*)?(([0-9]+) silver[a-z ]*)? coins`.} /calldll R Match {You must pay 4 gold and 5 silver coins.} /if {$Rerror==0 && $Rmatch==1} {/showme {$P2 $P4} {/showme {Incorrect}} $P2 = "4" $P2 = "5" /calldll R Match {You must pay 12 silver coins.} $P2 = "" $P4 = "12" ------------------------- Efficiency About 1000 matches per second on Celeron 400MHz. About 1000 Set per second on Celeron 400MHz. About 1000 iSet per second on Celeron 400MHz. iSet is more time consuming than Set. Try to use ^ and $.