如何以特定方式替换新行和换行符
How to replace the new line and line feed in a specific manner
我在 oracle.I 中名为 "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.This 行的文本 2.This 是第 行的文本。第 3 行到此结束。这是第 4.The 行的文本 行结束这里.
有人能说说如何实现吗?
您可以使用 '(' || 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,}',
'') as Result from dual
该模式匹配 2 个或更多 ({2,}
) 个连续重复的 CR 符号,后跟一个可选的(1 或零,?
)LF 符号或(|
)一个低频符号。
匹配被单个最后匹配的 CRLF、CR 或 LF 替换,因为 </code> 是捕获组 #1 捕获的值的占位符(第一个 <code>(...)
构造在模式中)。
online demo 的输出:
如果字段为空或NULL
使用以下内容。
SELECT DATA
FROM main_data
WHERE DATA IS NOT NULL OR DATA != ''
如何将连续出现的行尾标记替换为一个。
DECLARE
DATA VARCHAR2(2000);
BEGIN
DATA := 'This is a text from line 1.' || CHR(13) || CHR(13) ||
'This is text from line 2.' || CHR(13) || CHR(13) ||
'This is a text from line 3.The line 3 ends here .' || CHR(13) ||CHR(13) ||
'This is a text from line 4.The line ends here .';
dbms_output.put_line(regexp_replace(DATA, chr(13) || CHR(13), CHR(13)));
END;
这会给你
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 .
我在 oracle.I 中名为 "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.This 行的文本 2.This 是第 行的文本。第 3 行到此结束。这是第 4.The 行的文本 行结束这里.
有人能说说如何实现吗?
您可以使用 '(' || 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,}',
'') as Result from dual
该模式匹配 2 个或更多 ({2,}
) 个连续重复的 CR 符号,后跟一个可选的(1 或零,?
)LF 符号或(|
)一个低频符号。
匹配被单个最后匹配的 CRLF、CR 或 LF 替换,因为 </code> 是捕获组 #1 捕获的值的占位符(第一个 <code>(...)
构造在模式中)。
online demo 的输出:
如果字段为空或NULL
使用以下内容。
SELECT DATA
FROM main_data
WHERE DATA IS NOT NULL OR DATA != ''
如何将连续出现的行尾标记替换为一个。
DECLARE
DATA VARCHAR2(2000);
BEGIN
DATA := 'This is a text from line 1.' || CHR(13) || CHR(13) ||
'This is text from line 2.' || CHR(13) || CHR(13) ||
'This is a text from line 3.The line 3 ends here .' || CHR(13) ||CHR(13) ||
'This is a text from line 4.The line ends here .';
dbms_output.put_line(regexp_replace(DATA, chr(13) || CHR(13), CHR(13)));
END;
这会给你
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 .