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

pcreSubstitute

pcreSubstitute( 
[o_pcreObject]
t_string 
) 
=> t_result / nil

Description

If o_pcreObject is not provided, pcreSubstitute copies the input string and substitutes all pattern tags in it using the corresponding matched strings from the last pcreExecute/pcreMatch* operation.

If o_pcreObject is provided, pcreSubstitute copies the input string and substitutes all pattern tags in it using the corresponding matched strings from the last pcreExecute operation that used the given o_pcreObject.

Pattern tags are of the form \n, where n is 0-9. \0 (or &) refers to the string that matched the entire regular expression; \k refers to the string that matched the pattern wrapped by the kth backslash (...\) in the regular expression.

If o_pcreObject is provided, pattern tag can also have the next form \{x_num}, where x_num is a positive integer. This refers to the string that matches the pattern by the x_num(th) backslash (...\) in the regular expression which has been compiled to o_pcreObject. The matched string will be taken from the last string which was matched by pcreExecute using o_pcreObject.

Argument

o_pcreObject

An object that was used in pcreExecute.

t_string

Argument string to which the function applies the substitution.

Value Returned

t_result

Copy of the argument with the specified substitutions.

nil

The last string matching operation failed (none of the pattern tags are meaningful).

Examples

comPat = pcreCompile( "([a-z]+)\\.\\1" ) => pcreobj@0x27d048
pcreExecute( comPat "abc.bc" )
=> t
pcreSubstitute( "*\\0*" )
=> "*bc.bc*"
pcreSubstitute( "The matched string is: \\1" )
=> "The matched string is: bc"
r = pcreCompile("x[0-9]")
=> pcreobj@0x81ca018
pcreExecute(r "x1")
=> t
str1 = "\\0fff\\1ffff\\2fffff"
"\\0fff\\1ffff\\2fffff"
pcreSubstitute(str1)
=> "x1ffffffffffff"
pcre = pcreCompile("(a)(b+)([as]+)(q)(w)(r*)(t)(u)(i)(h)(k)(b).*")
=> pcreobj@0x83bb018
pcre1 = pcreCompile("0x([0-9]+)")
=> pcreobj@0x83bb034
pcreExecute(pcre "abbbasasssqwtuihkbdddd") 
=> t 
pcreExecute(pcre1 "0x333") 
=> t 
(for i 0 12
    str = (if i < 10 (sprintf nil "\\%d" i) (sprintf nil "\\{%d}" i))
    (printf "pcreSubstitute(pcre '%s') == '%L'\n" str pcreSubstitute(pcre str))
)
pcreSubstitute(pcre '\0') == '"abbbasasssqwtuihkbdddd"'
pcreSubstitute(pcre '\1') == '"a"'
pcreSubstitute(pcre '\2') == '"bbb"'
pcreSubstitute(pcre '\3') == '"asasss"'
pcreSubstitute(pcre '\4') == '"q"'
pcreSubstitute(pcre '\5') == '"w"'
pcreSubstitute(pcre '\6') == '""'
pcreSubstitute(pcre '\7') == '"t"'
pcreSubstitute(pcre '\8') == '"u"'
pcreSubstitute(pcre '\9') == '"i"'
pcreSubstitute(pcre '\{10}') == '"h"'
pcreSubstitute(pcre '\{11}') == '"k"'
pcreSubstitute(pcre '\{12}') == '"b"'
t
pcreSubstitute("the last pcreExecute was called - &") 
=>"the last pcreExecute was called - 0x333"

Related Topics

String Functions

pcreCompile

pcreExecute


Return to top
 ⠀
X