Difference between revisions of "String.match"
From GiderosMobile
(added another example) |
|||
(2 intermediate revisions by one other user not shown) | |||
Line 7: | Line 7: | ||
If found, returns any captures in the pattern. If no captures were specified the entire matching string is returned. If not found, returns nil. | If found, returns any captures in the pattern. If no captures were specified the entire matching string is returned. If not found, returns nil. | ||
− | < | + | <syntaxhighlight lang="lua"> |
(string) = string.match(string,pattern) | (string) = string.match(string,pattern) | ||
− | </ | + | </syntaxhighlight> |
'''note''': this is similar to string.find, except that the starting and ending index are not returned. | '''note''': this is similar to string.find, except that the starting and ending index are not returned. | ||
Line 20: | Line 20: | ||
=== Examples === | === Examples === | ||
− | < | + | <syntaxhighlight lang="lua"> |
print (string.match ("You see dogs and cats", "s..")) -- see | print (string.match ("You see dogs and cats", "s..")) -- see | ||
-- checks if a string contains any letters | -- checks if a string contains any letters | ||
local mystring = "0123456" | local mystring = "0123456" | ||
print(mystring:match("%a")) -- false | print(mystring:match("%a")) -- false | ||
− | </ | + | </syntaxhighlight> |
+ | |||
+ | '''Checks if a string ends with...''' | ||
+ | <syntaxhighlight lang="lua"> | ||
+ | local str = "the string ends with B" | ||
+ | if str:match("B$") then print("indeed") end | ||
+ | </syntaxhighlight> | ||
{{String}} | {{String}} |
Latest revision as of 13:37, 20 July 2025
Available since: Gideros 2011.6
Class: string
Description
Finds the first match of the regular expression "pattern" in "str", starting at position "index". The starting position (index) is optional, and defaults to 1 (the start of the string).
If found, returns any captures in the pattern. If no captures were specified the entire matching string is returned. If not found, returns nil.
(string) = string.match(string,pattern)
note: this is similar to string.find, except that the starting and ending index are not returned.
Parameters
string: (String) any string
pattern: (String) specifies the pattern to match
Return values
Returns (string) string matching pattern
Examples
print (string.match ("You see dogs and cats", "s..")) -- see
-- checks if a string contains any letters
local mystring = "0123456"
print(mystring:match("%a")) -- false
Checks if a string ends with...
local str = "the string ends with B"
if str:match("B$") then print("indeed") end