PregFind: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
m (→‎Syntax: Grammar?!)
m (grammar again)
Line 2: Line 2:
{{Server client function}}
{{Server client function}}
{{New feature/item|4.014|1.4|5106|
{{New feature/item|4.014|1.4|5106|
This function find only one match and when match is founded stop working and return a result.
This function stops at the first occurrence of the pattern in the input string and returns the result of the search.
}}
}}


Line 23: Line 23:
addCommandHandler( 'examples',
addCommandHandler( 'examples',
function( )
function( )
                 -- hello word find
                 -- find the first occurrence of 'hello world' in a string
outputDebugString( pregFind( 'hello world, hello world, hello world', 'hello world' ) and 'found' or 'not found' ) -- found  
outputDebugString( pregFind( 'hello world, hello world, hello world', 'hello world' ) and 'found' or 'not found' ) -- found  
                 -- integer number find
                 -- find the first occurrence of an integer in a string
                 outputDebugString( pregFind( '123', '^-{0,1}\\d+$' ) and 'found' or 'not found' ) -- found
                 outputDebugString( pregFind( '123', '^-{0,1}\\d+$' ) and 'found' or 'not found' ) -- found
                 -- Login with a-z and A-Z symbols and have more 3 symbols and without any spaces
                 -- check if the input string consists of at least 3 letters from a to z and A-Z and does not contain any whitespace characters
                 outputDebugString( pregFind( 'Kenix', '^[a-zA-Z]{3,}$' ) and 'found' or 'not found' ) -- found
                 outputDebugString( pregFind( 'Kenix', '^[a-zA-Z]{3,}$' ) and 'found' or 'not found' ) -- found
                 -- RP nickname check
                 -- check if the input string matches the format of a role-play name
                 outputDebugString( pregFind( 'Garry_Newman', '([A-Z]{1,1})[a-z]{2,9}_([A-Z]{1,1})[a-z]{2,9}' ) and 'found' or 'not found' ) -- found
                 outputDebugString( pregFind( 'Garry_Newman', '([A-Z]{1,1})[a-z]{2,9}_([A-Z]{1,1})[a-z]{2,9}' ) and 'found' or 'not found' ) -- found
                 -- Russian hello ( привет ) find
                 -- example of a search for non-ASCII characters (i.e. cyrillic) - привет
                 outputDebugString( pregFind( 'Всем привет парни, ещё раз привет :D', 'привет' ) and 'found' or 'not found' ) -- found
                 outputDebugString( pregFind( 'Всем привет парни, ещё раз привет :D', 'привет' ) and 'found' or 'not found' ) -- found
                 -- Sequence of numbers
                 -- example of a search for a specific sequence of numbers
                 outputDebugString( pregFind( '5, 10', '^([1-9]{1}[0-9]{0,})+(((,\s|,)[1-9]{1}[0-9]{0,}){0,1}){1,1}' ) and 'found' or 'not found' ) -- found
                 outputDebugString( pregFind( '5, 10', '^([1-9]{1}[0-9]{0,})+(((,\s|,)[1-9]{1}[0-9]{0,}){0,1}){1,1}' ) and 'found' or 'not found' ) -- found
end
end

Revision as of 10:50, 6 June 2013

ADDED/UPDATED IN VERSION 1.4 r5106:

This function stops at the first occurrence of the pattern in the input string and returns the result of the search.

Syntax

bool pregFind ( string subject, string pattern )

Required Arguments

Returns

Returns true if the pattern was found in the input string, false otherwise.

Example

Click to collapse [-]
Shared ( client and server )

Some examples:

addCommandHandler( 'examples',
	function( )
                -- find the first occurrence of 'hello world' in a string
		outputDebugString( pregFind( 'hello world, hello world, hello world', 'hello world' ) and 'found' or 'not found' ) -- found 
                -- find the first occurrence of an integer in a string
                outputDebugString( pregFind( '123', '^-{0,1}\\d+$' ) and 'found' or 'not found' ) -- found
                -- check if the input string consists of at least 3 letters from a to z and A-Z and does not contain any whitespace characters
                outputDebugString( pregFind( 'Kenix', '^[a-zA-Z]{3,}$' ) and 'found' or 'not found' ) -- found
                -- check if the input string matches the format of a role-play name
                outputDebugString( pregFind( 'Garry_Newman', '([A-Z]{1,1})[a-z]{2,9}_([A-Z]{1,1})[a-z]{2,9}' ) and 'found' or 'not found' ) -- found
                -- example of a search for non-ASCII characters (i.e. cyrillic) - привет
                outputDebugString( pregFind( 'Всем привет парни, ещё раз привет :D', 'привет' ) and 'found' or 'not found' ) -- found
                -- example of a search for a specific sequence of numbers
                outputDebugString( pregFind( '5, 10', '^([1-9]{1}[0-9]{0,})+(((,\s|,)[1-9]{1}[0-9]{0,}){0,1}){1,1}' ) and 'found' or 'not found' ) -- found
	end
)

Requirements

Minimum server version 1.4-9.05106
Minimum client version 1.4-9.05106

Note: Using this feature requires the resource to have the above minimum version declared in the meta.xml <min_mta_version> section. e.g. <min_mta_version server="1.4-9.05106" client="1.4-9.05106" />

See Also