string.match

From GMod Wiki

Jump to: navigation, search
Function
Syntax string.match( String string, String pattern[, Integer startindex ] )
Where is this used?
Description:
If it finds a match in the pattern, it returns that. Otherwise, it will return nil .
Returns: String [,String , String , ...]
Part of Library: String
Realm: NewerShared.png
BBCode Link: [b][url=http://wiki.garrysmod.com/?title=String.match]String.match [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]



Examples

DescriptionCheck's whether the string is a map or not.
Used onNewerShared.png
Code
local str = "test.bsp";
 
if( string.match(str, "(%.bsp)$") )then
	print( "This is a map!" );
else
	print( "This is not a map." );
end
Output"This is a map!" is printed in the console.
DescriptionShows the use of multiple captures.
Used onNewerShared.png
Code
local a = "one: two"
local one,two = string.match(a, "(%l*): (%l*)")
print("One: " .. one .. ", two: " .. two); 
OutputOne: one, two: two

Additional Notes

See Also

Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox