Skip to content

Instantly share code, notes, and snippets.

@arolle
Created August 15, 2014 20:20
Show Gist options
  • Save arolle/23627fb2a878060b6b63 to your computer and use it in GitHub Desktop.
Save arolle/23627fb2a878060b6b63 to your computer and use it in GitHub Desktop.
protects all regexp special characters
(:
: protects all regexp special characters
: @author Marc van Grootel
: @see http://www.mail-archive.com/basex-talk@mailman.uni-konstanz.de/msg04573.html
:)
declare function route:re-escape($string as xs:string)
as xs:string {
let $regexp-chars := ('.','*','+','|','?','(',')','[',']','{','}','^')
return
(: Note that '\' and '$' in the fold caused invalid pattern errors therefore put them in separate replace :)
fold-left(
$regexp-chars,
replace(replace($string, '\\', '\\\\'), '\$', '\\\$'),
function($a, $b) {
replace($a, '\'||$b, '\\'||$b )
}
)
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment