PregReplace: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Created page with "__NOTOC__ {{Server client function}} {{New feature/item|4.014|1.4|5106| This function replace all matches and return replaced string. }} ==Syntax== <syntaxhighlight lang="lua"> string pregRepl...")
 
(Remove obsolete Requirements section)
 
(15 intermediate revisions by 8 users not shown)
Line 1: Line 1:
__NOTOC__
__NOTOC__
{{Server client function}}
{{Server client function}}
{{New feature/item|4.014|1.4|5106|
{{New items|3.0135|1.3.5|
This function replace all matches and return replaced string.
This function performs a regular expression search and replace and returns the replaced string.
}}
}}
{{Warning|When declaring a pattern string in quotes, the backslash character should be doubled up. e.g. "\\(" will match a single bracket. This also applies to the replacement string.}}
{{Warning|Multiline flag does not work correctly}}


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


===Required Arguments===
===Required Arguments===
*'''base:''' The base [[string]] for replace.
*'''subject:''' The input [[string]].
*'''pattern:''' The pattern for replace in base string.
*'''pattern:''' The pattern [[string]] to search for in the input [[string]].
*'''replace:''' The [[string]] for replace.
*'''replacement:''' The replacement [[string]] to replace all matches within 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 replaced ''[[string]]'', ''false'' otherwise.
Returns the replaced ''[[string]]'', or [[bool]] ''false'' otherwise.
 


==Example==  
==Example==  
Line 23: Line 27:
Some examples:
Some examples:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
addCommandHandler( 'examples',
addCommandHandler('examples',
function( )
    function ()
                -- Replace doh to done
        -- Replace doh with done
outputDebugString( pregReplace( 'I doh this, guys.', 'doh', 'done' ) or 'not replaced' ) -- I done this, guys
        outputDebugString( pregReplace( 'I doh this, guys.', 'doh', 'done' ) or 'not replaced' ) -- Result: I done this, guys
                -- Replace all integer number to none string
 
                outputDebugString( pregReplace( 'AaaBbbZzz', '[A-Z]{1,}', '' ) or 'not replaced' ) -- aabbzz
        -- Remove all uppercase alphabetic characters
end
        outputDebugString( pregReplace( 'AaaBbbZzz', '[A-Z]{1,}', '' ) or 'not replaced' ) -- Result: aabbzz
 
        -- Use simple backreference in replacement string
        outputDebugString( pregReplace( "I love Lua!", "(Lua)", "Moon\\1" ) ) -- Result: I love MoonLua!
 
        -- Remove repeated characters
        outputDebugString( pregReplace( "Keeeeeep this shooooooort.", "((.)\\2{2})\\2+", "\\1" ) ) -- Result: Keeep this shooort.
    end
)
)
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>


==Requirements==
==Changelog==
{{Requirements|1.4-9.05106|1.4-9.05106|}}
{{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 performs a regular expression search and replace and returns the replaced string.

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

Syntax

string pregReplace ( string subject, string pattern, string replacement [, int/string flags ] )

Required Arguments

  • subject: The input string.
  • pattern: The pattern string to search for in the input string.
  • replacement: The replacement string to replace all matches within 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 the replaced string, or bool false otherwise.

Example

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

Some examples:

addCommandHandler('examples',
    function ()
        -- Replace doh with done
        outputDebugString( pregReplace( 'I doh this, guys.', 'doh', 'done' ) or 'not replaced' ) -- Result: I done this, guys

        -- Remove all uppercase alphabetic characters
        outputDebugString( pregReplace( 'AaaBbbZzz', '[A-Z]{1,}', '' ) or 'not replaced' ) -- Result: aabbzz

        -- Use simple backreference in replacement string
        outputDebugString( pregReplace( "I love Lua!", "(Lua)", "Moon\\1" ) ) -- Result: I love MoonLua!

        -- Remove repeated characters
        outputDebugString( pregReplace( "Keeeeeep this shooooooort.", "((.)\\2{2})\\2+", "\\1" ) ) -- Result: Keeep this shooort.
    end
)

Changelog

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

See Also