如何替换每个字符,除了之前具有特定字符的字符

How to replace each character like something, except those that have a certain character just before

我有这样的文字

'
-line1 //no space between line1 and 2
-line2

-line4

-line6
'

我希望文字变成这样(只有在'-'之前有空行的情况下才能在'-'之前添加分行)

'-line1 //now empty line between line1 and 2

-line2

-line4

-line6
'

我正在考虑使用 REGEXP_REPLACE。

SELECT REGEXP_REPLACE (mytext, '-', chr(13) || '-')

这个函数在每个-之后加一行。如果前面有空行,是否有避免替换的选项。

oracle regex 好像不支持 look-ahead 和 look-backs.
我们可以做你的替换,然后消除任何有 2 个连续空行的地方。我们在一行中查找三个换行符 \n = Chr(13) 的序列,将它们替换为两个。

SELECT 
  REGEXP_REPLACE (
    REGEXP_REPLACE (
       mytext,'-', '\n-'),
    '\n\n\n','\n\n')

\n\n\n也可以写成\n{3}.

您可以用两个换行符替换前面有 non-newline 字符且后跟 - 的任何换行符:

SELECT REGEXP_REPLACE('-first line
-line2

-line4

-line6', '([^' || chr(10) || '])' || chr(10) || '-', '' || chr(10) || chr(10) || '-') AS out
FROM dual

输出:

-first line

-line2

-line4

-line6

Demo on dbfiddle