string/match

From Garry's Mod
(Difference between revisions)
Jump to: navigation, search
m (fixed width example output)
m (Corrected incorrect type & description of returned value)
Line 20: Line 20:
 
}}
 
}}
 
{{Ret
 
{{Ret
|type=string
+
|type=vararg
|desc=Matched text
+
|desc=Matched text(s)
 
}}
 
}}
 
{{Example
 
{{Example

Revision as of 17:42, 26 August 2017

 string.match( )

Contents

Description

Finds a Pattern in a string.

Arguments

string string

String which should be searched in for matches.

Arguments

string pattern

The pattern that defines what should be matched.

Arguments

number startPosition

The start index to start the matching from, can be negative to start the match from a position relative to the end.

Returns

vararg

Matched text(s)

Examples

Example

local toMatch = "this is a sample text"
print( string.match( toMatch, "sample" ) )
-- regex works
print( string.match( toMatch, "^[a-z]" ) )
print( string.match( toMatch, "^this" ) )
print( string.match( toMatch, "^..is" ) )
print( string.match( toMatch, "text$" ) )
-- entire string
print( string.match( toMatch, "^.*$" ) )
-- nil
print( string.match( toMatch, "this$" ) )
print( string.match( toMatch, "nil" ) )

Output:

sample  
t  
this  
this  
text  
this is a sample text  
nil  
nil
Personal tools
Navigation