Utf8.match: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
m (tweaks)
 
Line 5: Line 5:


==Syntax==
==Syntax==
<syntaxhighlight lang="lua">mixed,... utf8.match ( string input, string pattern [, int index = 1 ] )</syntaxhighlight>
<syntaxhighlight lang="lua">string,... utf8.match ( string input, string pattern [, int index = 1 ] )</syntaxhighlight>


===Required Arguments===
===Required Arguments===
Line 13: Line 13:
===Optional Arguments===
===Optional Arguments===
{{OptionalArg}}
{{OptionalArg}}
*'''index:''' A number representing the beginning position for the pattern matching
*'''index:''' An integer representing the beginning position for the pattern matching


===Returns===
===Returns===
Returns a sequence of ''mixed'' type values (depends on the pattern), ''nil'' otherwise.
Returns a sequence of ''string'' matches from the '''input''' string, ''nil'' otherwise.


==Example==
==Example==

Latest revision as of 18:42, 15 February 2016

Extract substrings by matching patterns in the input string. This function can be used to extract specific information from a string.

Syntax

string,... utf8.match ( string input, string pattern [, int index = 1 ] )

Required Arguments

  • input: A string character sequence
  • pattern: A string match pattern

Optional Arguments

NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use. For more information on optional arguments, see optional arguments.

  • index: An integer representing the beginning position for the pattern matching

Returns

Returns a sequence of string matches from the input string, nil otherwise.

Example

Click to collapse [-]
Server

This example shows how to extract values from an input string by using a pattern to match the value parts.

local input = "Level: 5, Rank: General, 128.42 points"
local level, rank, points = utf8.match( input, "Level: (%d+), Rank: (.-), (%d+.?%d*) points" )
level, points = tonumber( level ), tonumber( points )

print( level, rank, points ) -- 5, General, 128.42

See Also