Description
The fn:replace function replaces parts of a string that match a regular expression. The regular expression syntax used is defined by XML Schema with a few modifications/additions in XQueryXPath/XSLT. The $pattern argument is a regular expression. While it is nice to have the power of regular expressions, if you simply want to replace a particular sequence of characters you don't have to be familiar with regular expressions to do that; you can just specify the string you want replaced for $pattern, as long as it doesn't contain any special characters.
The $replacement argument specifies a string (not a pattern) that is to be used as a replacement.
The $flags argument allows for additional options in the interpretation of the regular expression, such as multi-line processing and case insensitivity. Flags, reluctant quantifiers and sub-expressions are features that are covered in detail in chapter 18 of the book XQuery.
For more examples of XQueryXPath/XSLT/XML Schema regular expressions, see this page.
This description is © Copyright 2007, O'Reilly Media. It is excerpted from the book XQuery by Priscilla Walmsley, O'Reilly, 2007. For a complete explanation of this function, please refer to Appendix A of the book. Arguments and Return Type| Name | Type | Description |
$input |
xs:string? |
the string to change |
$pattern |
xs:string |
regular expression to match the areas to be replaced |
$replacement |
xs:string |
the replacement string |
$flags |
xs:string |
flags that control multiline mode, case insensitivity, etc. |
| return value |
xs:string |
Examples| XQuery Example | Results |
|---|
replace('query', 'r', 'as')
|
queasy
|
replace('query', 'qu', 'quack')
|
quackery
|
replace('query', '[ry]', 'l')
|
quell
|
replace('query', '[ry]+', 'l')
|
quel
|
replace('query', 'z', 'a')
|
query
|
replace('query', 'query', '')
|
zero-length string
|
replace( (), 'r', 'as')
|
zero-length string
|
replace('query', 'r?', 'as')
|
Error FORX0003 |
replace('query', '(r', 'as')
|
Error FORX0002 |
replace('Chapter', '(Chap)|(Chapter)', 'x')
|
xter
|
| The following examples show the difference between reluctant and regular quantifiers: |
replace('reluctant', 'r.*t', 'X')
|
X
|
replace('reluctant', 'r.*?t', 'X')
|
Xant
|
replace('aaah', 'a{2,3}', 'X')
|
Xh
|
replace('aaah', 'a{2,3}?', 'X')
|
Xah
|
replace('aaaah', 'a{2,3}', 'X')
|
Xah
|
replace('aaaah', 'a{2,3}?', 'X')
|
XXh
|
| The following examples exhibit the use of sub-expressions: |
replace('Chap 2...Chap 3...Chap 4...',
'Chap (\d)', 'Sec $1.0')
|
Sec 2.0...Sec 3.0...Sec 4.0...
|
replace('abc123', '([a-z])', '$1x')
|
axbxcx123
|
replace('2315551212',
'(\d{3})(\d{3})(\d{4})', '($1) $2-$3')
|
(231) 555-1212
|
replace('2006-10-18',
'\d{2}(\d{2})-(\d{2})-(\d{2})',
'$2/$3/$1')
|
10/18/06
|
replace('25', '(\d+)', '\$$1.00')
|
$25.00
|
See AlsoHistory |
|