我在 Oracle 中名为“DATA”的列中有以下数据。我试图删除每行后的新空行。
输入
This is a text from line 1. This is text from line 2. This is a text from line 3.The line 3 ends here . This is a text from line 4.The line ends here .
输出
This is a text from line 1. This is text from line 2. This is a text from line 3.The line 3 ends here . This is a text from line 4.The line ends here .
我尝试过使用
查询:
Select regexp_replace(regexp_replace(data,chr(13),null),chr(10),null) from main_data;
当我执行以下查询时,输出就像一个段落。
这是第 1 行的文本。这是第 2 行的文本。这是第 3 行的文本。第 3 行在此结束。这是第 4 行的文本。该行在此结束。
谁能说说如何实现这一目标吗?
最佳答案
您可以使用 '(' || chr(13) || chr(10) || '?|' || chr(10) || '){2,}'
正则表达式:
select regexp_replace(
'This is a text from line 1.' || chr(13) || chr(10) || chr(13) || chr(10) || 'This is text from line 2.' || chr(10) || chr(10) || 'This is a text from line 3.The line 3 ends here .' || chr(10) || chr(10) || 'This is a text from line 4.The line ends here .',
'(' || chr(13) || chr(10) || '?|' || chr(10) || '){2,}',
'\1') as Result from dual
该模式匹配 2 个或多个 ({2,}
) 连续重复的 CR 符号,后跟可选的(1 或零,?
)LF 符号或 ( |
) LF 符号。
匹配项将替换为最后一次匹配的 CRLF、CR 或 LF,因为 \1
是捕获组 #1 捕获的值的占位符(第一个 ( ...)
在模式中构造)。
online demo 的输出:
关于regex - 如何以特定方式替换新行和换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50943511/