Difference between revisions of "String.match"

From GiderosMobile
m (Text replacement - "</source>" to "</syntaxhighlight>")
 
(8 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
 
'''Available since:''' Gideros 2011.6<br/>
 
'''Available since:''' Gideros 2011.6<br/>
 +
'''Class:''' [[string]]<br/>
 +
 
=== Description ===
 
=== Description ===
<translate>Find the first match of the regular expression "pattern" in "str", starting at position "index".
+
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).
  
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.
 +
<syntaxhighlight lang="lua">
 +
(string) = string.match(string,pattern)
 +
</syntaxhighlight>
 +
'''note''': this is similar to string.find, except that the starting and ending index are not returned.
  
If found, returns any captures in the pattern. If no captures were specified the entire matching string is returned.
+
=== Parameters ===
 +
'''string''': (String) any string<br/>
 +
'''pattern''': (String) specifies the pattern to match<br/>
  
If not found, returns nil.
+
=== Return values ===
 
+
'''Returns''' (string) string matching pattern<br/>
This is similar to string.find, except that the starting and ending index are not returned.
 
  
 +
=== Examples ===
 +
<syntaxhighlight lang="lua">
 +
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
 +
</syntaxhighlight>
  
print (string.match ("You see dogs and cats", "s..")) --> see</translate>
+
{{String}}
<source lang="lua">
 
(string) = string.match(string,pattern)
 
</source>
 
=== Parameters ===
 
'''string''': (String) <translate>Any string.</translate> <br/>
 
'''pattern''': (String) <translate>Specifies the pattern to match.</translate> <br/>
 
=== Return values ===
 
'''Returns''' (string) <translate>String matching pattern.</translate><br/>
 

Latest revision as of 15:33, 13 July 2023

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