m
Move page script moved page Pattern matching to WoW:Pattern matching without leaving a redirect
(overall tweaks) |
m (Move page script moved page Pattern matching to WoW:Pattern matching without leaving a redirect) |
||
| (8 intermediate revisions by 7 users not shown) | |||
| Line 1: | Line 1: | ||
{{wowlua}} | |||
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"). | 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"). | ||
| Line 5: | Line 6: | ||
Lua has a few string functions that use patterns (what some people mistakenly call regexes). Among them are | 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 | * [[API_strfind|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.gfind(string, pattern) -- when called repeatedly, finds each successive instance of pattern in string | ||
* string. | * [[API gsub|string.gmatch]](string, pattern, repl) -- returns a string where all instances of pattern in string have been replaced with repl | ||
* string.match(string, pattern, | * [[API strmatch|string.match]](string, pattern, init) -- Returns a list of matches of pattern in string, starting search at init (1 is the first character of string, 2 is the second, etc.). | ||
== Constructing a pattern == | == Constructing a pattern == | ||
| Line 27: | Line 28: | ||
* %x --- represents all hexadecimal digits. | * %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. | * %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 | The upper cased version of the above reverses the meaning of said (i.e. %A --- represents all ''non''-letters). | ||
* [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 | * %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. | * [^set] --- represents the complement of set, where set is interpreted as above. | ||
| Line 49: | Line 51: | ||
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. | 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. | ||
== Pattern Items == | |||
* a single character class, which matches any single character in the class; | |||
* a single character class followed by `*´, which matches 0 or more repetitions of characters in the class. These repetition items will always match the longest possible sequence; | |||
* a single character class followed by `+´, which matches 1 or more repetitions of characters in the class. These repetition items will always match the longest possible sequence; | |||
* a single character class followed by `-´, which also matches 0 or more repetitions of characters in the class. Unlike `*´, these repetition items will always match the shortest possible sequence; | |||
* a single character class followed by `?´, which matches 0 or 1 occurrence of a character in the class; | |||
* %n, for n between 1 and 9; such item matches a substring equal to the n-th captured string (see below); | |||
* %bxy, where x and y are two distinct characters; such item matches strings that start with x, end with y, and where the x and y are balanced. This means that, if one reads the string from left to right, counting +1 for an x and -1 for a y, the ending y is the first y where the count reaches 0. For instance, the item %b() matches expressions with balanced parentheses. | |||
[http://www.lua.org/manual/5.0/manual.html#5.3 Source] | |||
== Example == | == 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) | 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) | ||
| Line 65: | Line 76: | ||
[http://www.lua.org/manual/5.0/manual.html#5.3 Lua.org] - Good manual | [http://www.lua.org/manual/5.0/manual.html#5.3 Lua.org] - Good manual | ||
[http://lua-users.org/wiki/PatternsTutorial Lua-users.org] - Wiki | [http://lua-users.org/wiki/PatternsTutorial Lua-users.org] - Wiki | ||
[[Category:Glossary]] | [[Category:Glossary]] | ||
[[Category:HOWTOs]] | [[Category:HOWTOs]] | ||
[[Category:Lua]] | |||