UPDATE contentitem
SET "content" = regexp_replace("content", '(\n)', '<br/>')
WHERE
isdeleted = FALSE
AND "content" ~* '\n';

 

用到的主要知识点有:

正则匹配:

操作符 描述 例子
~ 匹配正则表达式,大小写相关 'thomas' ~ '.*thomas.*'
~* 匹配正则表达式,大小写无关 'thomas' ~* '.*Thomas.*'
!~ 不匹配正则表达式,大小写相关 'thomas' !~ '.*Thomas.*'
!~* 不匹配正则表达式,大小写无关 'thomas' !~* '.*vadim.*'

 

函数:regexp_replace(string text, pattern text, replacement text [, flags text])
说明:如果没有匹配 pattern 的子字串,那么返回不加修改的source 字串。 如果有匹配,则返回的 source 字串里面的对应子字串将被 replacement 字串替换掉。replacement 字串可以包含 \n, 这里的 n 是 1 到 9, 表明源字串里匹配模式里第 n 圆括弧子表达式的部分将插入在该位置, 并且它可以包含 \& 表示应该插入匹配整个模式的字串。 如果你需要放一个文本杠在替换文本里,那么写 \\

例子:regexp_replace('Thomas', '.[mN]a.', 'M') = ThM

 

函数:replace(string text, from text, to text)
说明:Replace all occurrences in string of substring from with substring to 将字符的某一子串替换成另一子串
例子:('abcdefabcdef', 'cd', 'XX') = abXXefabXXef

 

相关文章: