home contribute faq download

FunctX XQuery Functions

functx:escape-for-regex

Escapes regex special characters

Google
Webxqueryfunctions.com

Description

The functx:escape-for-regex function escapes a string that you wish to be taken literally rather than treated like a regular expression. This is useful when, for example, you are calling the built-in fn:replace function and you want any periods or parentheses to be treated like literal characters rather than regex special characters.

Arguments and Return Type

NameTypeDescription
$arg xs:string? the string to escape
return value xs:string

XQuery Function Declaration

See XSLT definition.
XQuery Syntax for July 2004 - January 2007 (1.0):
declare namespace functx = "http://www.functx.com"; 
declare function functx:escape-for-regex 
  ( $arg as xs:string? )  as xs:string {
       
   replace($arg,
           '(\.|\[|\]|\\|\||\-|\^|\$|\?|\*|\+|\{|\}|\(|\))','\\$1')
 } ;
XQuery Syntax for May 2003:
declare namespace functx = "http://www.functx.com" 
define function escape-for-regex 
  ( $arg as xs:string? )  as xs:string {
       
   fn:replace($arg,
           '(\.|\[|\]|\\|\||\-|\^|\$|\?|\*|\+|\{|\}|\(|\))','\$1')
 }

Examples

XQuery ExampleResults
functx:escape-for-regex('5.55')
5\.55
functx:escape-for-regex('[abc]')
\[abc\]

See Also

fn:replaceReplaces parts of a string that match a regular expression

History

Published OnLast UpdatedContributor(s)
2006-06-272007-02-26Priscilla Walmsley, Datypic, pwalmsley@datypic.com, http://www.datypic.com
Need XQuery Help?
D A T Y P I C
Training | Consulting | Development

XQuery by Priscilla WalmsleyGet the book!
XQuery by Priscilla Walmsley