WoW:API strmatch: Difference between revisions
Jump to navigation
Jump to search
m (Created page with 'strmatch(string, pattern[, initpos]) - Similar to strfind but only returns the matches, not the string positions.') |
({{luaapi}}) |
||
| Line 1: | Line 1: | ||
{{ | {{luaapi}} | ||
Extract substrings by matching against a pattern. | Extract substrings by matching against a pattern. | ||
match [, match2, ...] = string.match(string, pattern[, initpos]) | |||
match1[, match2, ...] = strmatch(string, pattern[, initpos]) | match1[, match2, ...] = strmatch(string, pattern[, initpos]) | ||
== Arguments == | == Arguments == | ||
; string : String - The string to examine. | |||
; pattern : String - The pattern to search for within '''string'''. This pattern is similar to Unix regular expressions, but is not the same -- see Lua [[Pattern matching]] for more details. | |||
; initpos : Integer - Index of the character within '''string''' to begin searching. As is usual for Lua string functions, 1 refers to the first character of the string, 2 to the second, etc. -1 refers to the last character of the string, -2 to the second last, etc. If this argument is omitted, it defaults to 1; i.e., the search begins at the beginning of '''string'''. | |||
== Returns == | == Returns == | ||
; match1, match2, etc. : String - The matched substring(s) found within string. Multiple return values can occur. | |||
== Discussion == | == Discussion == | ||
| Line 29: | Line 29: | ||
2, "questions" | 2, "questions" | ||
Revision as of 23:23, 25 March 2010
← WoW Lua
Extract substrings by matching against a pattern.
match [, match2, ...] = string.match(string, pattern[, initpos]) match1[, match2, ...] = strmatch(string, pattern[, initpos])
Arguments
- string
- String - The string to examine.
- pattern
- String - The pattern to search for within string. This pattern is similar to Unix regular expressions, but is not the same -- see Lua Pattern matching for more details.
- initpos
- Integer - Index of the character within string to begin searching. As is usual for Lua string functions, 1 refers to the first character of the string, 2 to the second, etc. -1 refers to the last character of the string, -2 to the second last, etc. If this argument is omitted, it defaults to 1; i.e., the search begins at the beginning of string.
Returns
- match1, match2, etc.
- String - The matched substring(s) found within string. Multiple return values can occur.
Discussion
This function is similar to strfind, except that strmatch returns the actual matched strings, whereas strfind returns the starting and ending character positions, and then only those of the first match found.
Multiple return values occur when there are more than one parenthesized section within pattern. Parentheses set off each section to be returned.
If no match is found, returns nil.
Example
> = string.match("I have 2 questions for you.", "%d+ %a+")
2 questions
> = string.format("%d, %q", string.match("I have 2 questions for you.", "(%d+) (%a+)"))
2, "questions"