Garry's Mod Wiki

Revision Difference

string.match#511991

<function name="match" parent="string" type="libraryfunc">⤶ <description>Finds a [Pattern](/gmod/Patterns) in a string.</description>⤶ <realm>Shared and Menu</realm>⤶ <args>⤶ <arg name="string" type="string">String which should be searched in for matches.</arg>⤶ <arg name="pattern" type="string">The pattern that defines what should be matched.</arg>⤶ <arg name="startPosition" type="number" default="1">The start index to start the matching from, can be negative to start the match from a position relative to the end.</arg>⤶ </args>⤶ <rets>⤶ <ret name="" type="vararg">Matched text(s)</ret>⤶ </rets>⤶ </function>⤶ ⤶ <example>⤶ <code>⤶ 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" ) )⤶ </code>⤶ <outputfixedwidth>Fixed width</outputfixedwidth>⤶ <output>⤶ sample ⤶ t ⤶ this ⤶ this ⤶ text ⤶ this is a sample text ⤶ nil ⤶ nil⤶ </output>⤶ ⤶ </example>