Meaningless Drivel is fun!
The moose likes Oracle/OAS and the fly likes Oracle and regular expressions, linefeeds Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Oracle/OAS
Bookmark "Oracle and regular expressions, linefeeds" Watch "Oracle and regular expressions, linefeeds" New topic

Oracle and regular expressions, linefeeds

D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
In our oracle db, there is a varchar column in which I want to replace all linefeeds (or carriage return/linefeeds) with "<br/>" (we're changing a textfield for use with a html editor). I looked up the REGEXP_REPLACE function of oracle which seems like it should be able to do that, but I can't seem to get the reg exp right...I tried

REGEXP_REPLACE(column_name, '(\r?\n)', '<br/>')

but I see no replacement being done. I think I got the basic syntax right as I can replace normal characters. I also tried writing the linefeed in different ways, like \\n or \x0a but these don't seem right either. Has anyone done this before perhaps and can tell me the right syntax, documentation I saw says it should be \r and \n but these simply don't work for me
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
I solved this differently now, using the normal REPLACE function of oracle:

update <table> set <column>=REPLACE(<column>, CHR(13)) where ...;
update <table> set <column>=REPLACE(<column>, CHR(10), '<br/>') where ...;

First, carriage return characters are removed. Then, linefeed characters are replaced by <br/>. This seems to work fine.

Still interested if anyone knows the correct syntax for a regular expression tho.
I agree. Here's the link:
subject: Oracle and regular expressions, linefeeds
It's not a secret anymore!