WoW:Pattern matching
A regular expression is a formula for matching strings that follow some pattern. The formula (and syntax used) varies depending on the method of searching used. Lua, the scripting language that World of Warcraft uses, does not support regular expressions. Instead, it offers a basic pattern-matching mechanism with most of the features of REs. Though since Lua doesn't implement regexes, at all, but rather "patterns", this can confuse more than it helps. Better refer to the official Lua documentation (which never mentions "regular expressions").
"Regexes" (patterns) in Lua
- Main article: Lua functions#String Functions
Lua has a few string functions that use patterns (what some people mistakenly call regexes). Among them are
- string.find(string, pattern) -- finds the first instance of pattern in string
- string.gfind(string, pattern) -- when called repeatedly, finds each successive instance of pattern in string
- string.gsub(string, pattern, repl) -- returns a string where all instances of pattern in string have been replaced with repl
- string.match(string, pattern, limit) -- returns a list of matches up to limit or all of them if no limit passed
Constructing a pattern
So, how do we construct a pattern?
At the foundation of patterns are character classes. These classes represent sets of characters. Lua's classes are
- x (where x is not one of the magic characters ^$()%.[]*+-?) --- represents the character x itself.
- . --- represents all characters.
- %a --- represents all letters.
- %c --- represents all control characters.
- %d --- represents all digits.
- %l --- represents all lowercase letters.
- %p --- represents all punctuation characters.
- %s --- represents all space characters.
- %u --- represents all uppercase letters.
- %w --- represents all alphanumeric characters.
- %x --- represents all hexadecimal digits.
- %z --- represents the character with representation 0. Note that embedded zeroes in a pattern will not work. Use this instead.
- %x (where x is any non-alphanumeric character) --- represents the character x. This is the standard way to escape the magic characters. Any punctuation character (even the non magic) can be preceded by a `%´ when used to represent itself in a pattern.
- [set] --- represents the class which is the union of all characters in set. A range of characters may be specified by separating the end characters of the range with a `-´. All classes %x described above may also be used as components in set. All other characters in set represent themselves. For example, [%w_] (or [_%w]) represents all alphanumeric characters plus the underscore.
- [^set] --- represents the complement of set, where set is interpreted as above.
All patterns are made up of a sequence of these classes. For example, if you wanted to match the string "###Abc" (# designates a digit), you would use the pattern "%d%d%dAbc". By adding modifiers, you can change the meaning of these classes. Let x be the class we want to modify.
- x* -- matches 0 or more repetitions of x. Will always match the longest possible chain.
- x+ -- matches 1 or more repetitions of x. Will always match the longest possible chain.
- x- -- matches 0 or more repetitions of x. Will always match the shortest possible chain.
- x? -- matches 0 or 1 occurrence of x.
There are also four special pattern elements
- %n -- n must be a number between 1 and 9. Matches the nth captured substring (see below)
- %bxy -- matches a substring starting with x and ending with y. The substring must also have the same number of x and y.
- ^ -- When at the beginning of a pattern, it forces the pattern to match the beginning of a string
- $ -- When at the end of a pattern, it forces the pattern to match the end of a string
When ^ or $ is anywhere else in a pattern, it has no special meaning.
Patterns may also have subpatterns. These are enclosed with () and are counted as matches are found. They can be accessed using the %n element mentioned above. Using the empty capture "()" will return the string position where a match is found.
Example
Say we're writing a function that watches the combat chat and counts the number of times a murloc dies nearby (don't we all want this? :) ). So, how do we do this? (incidentally, I have done this. It was quite satisfying)
For simplicity's sake, let's assume all murlocs have the word "Murloc" in their name. For this case, the pattern "Murloc" would be all we need. However, as we all know, there seem to be no end to the varieties of Murlocs. Say we want to count all the murlocs killed nearby while we're in Darkshore. Now matching each type by name exactly would work, but would result in a huge function. It becomes simpler when we notice that they all (save the named ones) are of the "Greymist" clan, as we can use "Greymist" as our pattern.
Now these are extremely simple patterns. For a better challenge, let's go one step further. Instead of just counting the number that die, let's replace the name of whatever died with "An unholy demon spawn". Since the name of the mob will vary, we need a pattern that will match all of them. To do this, we can use "^Greymist .* dies()". This pattern will match strings that start with the word "Greymist" followed by any number of characters followed by the word "dies". Then we can just use the string.gsub function to do the replacement like so:
string.gsub(chatstring, "^Greymist .* dies", "An unholy demon spawn dies");
Afterthoughts
Hopefully this has given you a good start into how to use regular expressions. There are many many resources online regarding them, and if there's anything not covered here, you should be able to find it somewhere out there.
External links
Regular Expressions Explained - Good introduction to regexes Lua.org - Good manual Lua-users.org - Wiki