regexReplace: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
(added example for upper and lower case conversion)
m (Some wiki formatting)
 
(2 intermediate revisions by the same user not shown)
Line 8: Line 8:
|gr1= Strings - Regular Expression
|gr1= Strings - Regular Expression


|s1= haystack [[{{PAGENAMEE}}]] [pattern, replace string]
|s1= haystack [[regexReplace]] [pattern, replaceString]


|p1= haystack: [[String]]
|p1= haystack: [[String]]


|p2= pattern: [[String]] - Format [[Arma 3: Regular Expressions|regex pattern]]
|p2= pattern: [[String]] - format [[Arma 3: Regular Expressions|regex pattern]]


|p3= replaceString: [[String]] - {{Link|https://www.boost.org/doc/libs/1_76_0/libs/regex/doc/html/boost_regex/format/perl_format.html|Reference}}
|p3= replaceString: [[String]] - {{Link|https://www.boost.org/doc/libs/1_76_0/libs/regex/doc/html/boost_regex/format/perl_format.html|Reference}}


|r1= [[String]] - Modified text
|r1= [[String]] - modified text


|x1= <sqf>"wookie boOkie cookie" regexReplace [".oo/gio", "[$']"]; // "[kie boOkie cookie]kie [kie cookie]kie [kie]kie"</sqf>
|x1= <sqf>"wookie boOkie cookie" regexReplace [".oo/gio", "[$']"]; // "[kie boOkie cookie]kie [kie cookie]kie [kie]kie"</sqf>


|x2= <sqf>"wookie boOkie cookie" regexReplace [".oo/gio", "[$&]"]; // "[woo]kie [boO]kie [coo]kie"</sqf>
|x2= <sqf>"wookie boOkie cookie" regexReplace [".oo/gio", "[$&]"]; // "[woo]kie [boO]kie [coo]kie"</sqf>


|x3= <sqf>"wook1e boOk2e cook3e" regexReplace [".oo/gio", "[$`]"]; // "[]kie [k1e ]k2e [k2e ]k3e"</sqf>
|x3= <sqf>"wook1e boOk2e cook3e" regexReplace [".oo/gio", "[$`]"]; // "[]kie [k1e ]k2e [k2e ]k3e"</sqf>


|x4= <sqf>"wook1e boOk2e cook3e" regexReplace [".(oo)(.*?)e", "[$2]"]; // "[k1] [k2] [k3]"</sqf>
|x4= <sqf>"wook1e boOk2e cook3e" regexReplace [".(oo)(.*?)e", "[$2]"]; // "[k1] [k2] [k3]"</sqf>


|x5= <sqf>"wOokie boOkie cookie" regexReplace [".(?<test>oo)kie/gio", "[$+{test}]"]; // "[oo] [oO] [oo]"</sqf>
|x5= <sqf>"wOokie boOkie cookie" regexReplace [".(?<test>oo)kie/gio", "[$+{test}]"]; // "[oo] [oO] [oo]"</sqf>


|x6= <sqf>// Make captured group all lower case '\L' (lower case) or '\U' (upper case)
|x6= <sqf>
"ArmA 3 is so awesome!" regexReplace ["(rmA 3)", "\L$1"]; // "Arma 3 is so awesome!"</sqf>
// Make captured group all lower case '\L' (lower case) or '\U' (upper case)
"ArmA 3 is so awesome!" regexReplace ["(rmA 3)", "\L$1"]; // "Arma 3 is so awesome!"
</sqf>


|seealso= [[regexMatch]] [[regexFind]]
|seealso= [[regexMatch]] [[regexFind]]
}}
}}

Latest revision as of 18:14, 8 November 2023

Hover & click on the images for description

Description

Description:
Searches given haystack with given pattern and replaces all matches with given replaceString.
Groups:
Strings - Regular Expression

Syntax

Syntax:
haystack regexReplace [pattern, replaceString]
Parameters:
haystack: String
pattern: String - format regex pattern
replaceString: String - Reference
Return Value:
String - modified text

Examples

Example 1:
"wookie boOkie cookie" regexReplace [".oo/gio", "[$']"]; // "[kie boOkie cookie]kie [kie cookie]kie [kie]kie"
Example 2:
"wookie boOkie cookie" regexReplace [".oo/gio", "[$&]"]; // "[woo]kie [boO]kie [coo]kie"
Example 3:
"wook1e boOk2e cook3e" regexReplace [".oo/gio", "[$`]"]; // "[]kie [k1e ]k2e [k2e ]k3e"
Example 4:
"wook1e boOk2e cook3e" regexReplace [".(oo)(.*?)e", "[$2]"]; // "[k1] [k2] [k3]"
Example 5:
"wOokie boOkie cookie" regexReplace [".(?<test>oo)kie/gio", "[$+{test}]"]; // "[oo] [oO] [oo]"
Example 6:
// Make captured group all lower case '\L' (lower case) or '\U' (upper case) "ArmA 3 is so awesome!" regexReplace ["(rmA 3)", "\L$1"]; // "Arma 3 is so awesome!"

Additional Information

See also:
regexMatch regexFind

Notes

Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord or on the Forums.
Only post proven facts here! Add Note