PregFind: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Created page with "__NOTOC__ {{Server client function}} This function find only one match and when match is founded stop working and return a result. ==Syntax== <syntaxhighlight lang="lua"> bool pregFind ( strin...")
 
(Remove obsolete Requirements section)
 
(26 intermediate revisions by 7 users not shown)
Line 1: Line 1:
__NOTOC__
__NOTOC__
{{Server client function}}
{{Server client function}}
This function find only one match and when match is founded stop working and return a result.
{{New items|3.0135|1.3.5|
This function stops at the first occurrence of the pattern in the input string and returns the result of the search.
}}
{{Warning|When declaring a pattern string in quotes, the backslash character should be doubled up. e.g. "\\(" will match a single bracket.}}
{{Warning|Multiline flag does not work correctly}}


==Syntax==
==Syntax==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
bool pregFind ( string base, string pattern )
bool pregFind ( string subject, string pattern [, int/string flags ] )
</syntaxhighlight>
</syntaxhighlight>


===Required Arguments===
===Required Arguments===
*'''base:''' The base [[string]] for find match.
*'''subject:''' The input [[string]]
*'''pattern:''' The pattern for find in base string.
*'''pattern:''' The pattern [[string]] to search for in the input [[string]].
 
===Optional Arguments===
*'''flags:''' Conjuncted value that contains flags ( 1 - ignorecase, 2 - multiline, 4 - dotall, 8 - extended, 16 - unicode ) or ( i - Ignore case, m - Multiline, d - Dotall, e - Extended, u - Unicode )


===Returns===
===Returns===
Returns an ''[[bool]]'' if the function was successful found one match, ''false'' otherwise.
Returns ''true'' if the pattern was found in the input string, ''false'' otherwise.
 


==Example==  
==Example==  
<section name="Client" class="client" show="true">
<section name="Shared ( client and server )" class="both" show="true">
Some examples:
Some examples:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
addCommandHandler( 'examples',
addCommandHandler( 'examples',
function( )
function( )
                 -- hello words find
                 -- find the first occurrence of 'hello world' in a string
outputDebugString( pregFind( 'hello world, hello world, hello world', 'hello world' ) or 'not found' ) -- true
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+$' ) or 'not found' ) -- true
                 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
                 -- check if the input string consists of at least 3 letters from a to z (both uppercase and lowercase) and does not contain any whitespace characters
                 outputDebugString( pregFind( 'Kenix', '[a-zA-Z]{3,}' ) or 'not found' ) -- true
                 outputDebugString( pregFind( 'Kenix', '^[a-zA-Z]{3,}$' ) and 'found' or 'not found' ) -- found
                 -- Russian hello ( привет ) find
                 -- check if the input string matches the format of a role-play name
                 outputDebugString( pregFind( 'привет привет', 'привет' ) or 'not found' ) -- true
                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
                 -- Sequence of numbers
                -- example of a search for non-ASCII characters (i.e. cyrillic) - привет
                 outputDebugString( pregFind( '5, 10', '^([1-9]{1}[0-9]{0,})+(((,\s|,)[1-9]{1}[0-9]{0,}){0,1}){1,1}' ) or 'not found' ) -- true
                 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
end
)
)
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>
==Changelog==
{{ChangelogHeader}}
{{ChangelogItem|1.5.0-9.07315|Added flag "u" in regular expressions}}
==See Also==
==See Also==
{{Utility_functions}}
{{Utility_functions}}

Latest revision as of 17:11, 7 November 2024

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

[[|link=|]] Warning: When declaring a pattern string in quotes, the backslash character should be doubled up. e.g. "\\(" will match a single bracket.
[[|link=|]] Warning: Multiline flag does not work correctly

Syntax

bool pregFind ( string subject, string pattern [, int/string flags ] )

Required Arguments

Optional Arguments

  • flags: Conjuncted value that contains flags ( 1 - ignorecase, 2 - multiline, 4 - dotall, 8 - extended, 16 - unicode ) or ( i - Ignore case, m - Multiline, d - Dotall, e - Extended, u - Unicode )

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 (both uppercase and lowercase) 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
)

Changelog

Version Description
1.5.0-9.07315 Added flag "u" in regular expressions

See Also