Using Sub-Expressions with Replacement Variables
The replace
function allows parenthesized sub-expressions (also known as groups) to be referenced by number in the replacement string. In the $replacement
string, you can use the variables $1
, $2
, $3
, etc. to represent (in order) the parenthesized expressions in $pattern
. This is very useful when replacing strings on the condition that they come directly before or after another string—for example, if you want to change instances of the word Chap to the word Sec, but only those that are followed by a space and a digit. This technique can also be used to reformat data for presentation. Table 18-16 shows some examples.
Get XQuery now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.