Abhendra Singh
Abhendra Singh

Reputation: 1957

Regex for Special Characters in XQuery

Hi I am a java developer and from last few days I am working on OSB. I need to remove all spacial characters from a string. For this I tried following code in Assign block.

if(exists($Variable) and not(empty($Variable/text())))then 
fn:replace(fn:replace(fn:replace(fn:replace(
    fn:replace($Variable/text(),'[$`,:%!@#_-|]',''), 
    '[$?~@#!%:;=_+*]', ''), '[.]', ''), '[-]', ''), '[+= ?;]', '')
else 
    $Variable

But I want to do this in a single Regex. All these characters are not working in a single Regex string. Is there any way to do this in single Regex string?

I found a method functx:escape-for-regex but it is not working. Currently I am using Oracle 10g.

I want something like this:

"[^\\w-]|[$_-]" //It works in Java

Thanks in advance.

Upvotes: 0

Views: 4248

Answers (1)

Jens Erat
Jens Erat

Reputation: 38702

There's no need for regular expressions to remove some characters from a blacklist, just use translate(...):

translate($string, '-$`,:%!@#_|]$?~@#!%:;=_+*.-+= ?;', '')

I didn't clean up the character list, though.

The problem with your regexes is the dash in the list: [$`,:%!@#_-|]. This regex is blocking all characters from _ (character 95) to | (character 124). This includes all lower-case letters! Always put the dash first: [-$`,:%!@#_|].

Anyway: If possible, better use a whitelist. You will always be forgetting some characters. What about curly brackets?

Upvotes: 1

Related Questions