Product Documentation
Cadence SKILL Language Reference
Product Version IC23.1, November 2023

pcreReplace

pcreReplace( 
o_comPatObj 
t_source 
t_replacement 
x_index 
[ x_options ] ) 
=> t_result / t_source

Description

Replaces one or all occurrences of a previously-compiled regular expression in the given source string with the specified replacement string. The integer index indicates which of the matching substrings to replace. If the index is less than or equal to zero, the function applies the replacement string to all matching substrings. You can use an optional argument to specify independent option bits for controlling pattern matching. The matching algorithm is PCRE/Perl-compatible.

Arguments

o_comPatObj

Data object containing the compiled pattern returned from a previous pcreCompile call.

t_source

Source string to be matched and replaced.

t_replacement

Replacement string. You can use pattern tags in this string (see pcreSubstitute).

x_index

Integer index indicating which of the matching substrings to replace. If the index is less than or equal to zero, the function applies the replacement string to all matching substrings.

x_options

(Optional) Independent option bits that affect pattern matching. Valid values for this argument are the same as those for the x_options argument to the pcreExecute SKILL function.

Value Returned

t_result

Copy of the source string with the specified replacement (determined by the integer index).

t_source

Original source string if no match was found.

Examples

comPat1 = pcreCompile( "[0-9]+" ) 
=> pcreobj@0x27d258
pcreReplace( comPat1 "abc-123-xyz-890-wuv" "(*)" 0 )
=> "abc-(*)-xyz-(*)-wuv"
pcreReplace( comPat1 "abc-123-xyz-890-wuv" "(*)" 1 )
=> "abc-(*)-xyz-890-wuv"
pcreReplace( comPat1 "abc-123-xyz-890-wuv" "(*)" 2 )
=> "abc-123-xyz-(*)-wuv"
pcreReplace( comPat1 "abc-123-xyz-890-wuv" "(*)" 3 )
=> "abc-123-xyz-890-wuv"
comPat2 = pcreCompile( "xyz" ) => pcreobj@0x27d264 
pcreReplace( comPat2 "xyzzyxyzz" "xy" 0 )
=> "xyzyxyz"

Related Topics

String Functions

pcreCompile


Return to top
 ⠀
X