regex - 如何以特定方式替换新行和换行

标签 regex oracle oracle11g regexp-replace

我在 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 的输出:

enter image description here

关于regex - 如何以特定方式替换新行和换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50943511/

相关文章:

regex - 在JSF 2/PrimeFaces中使用正则表达式进行电子邮件验证

正则表达式:将单词开头的 <tags> 中的小写字母替换为大写字母

Oracle SQL 中的 java.sql.Timestamp 到日期转换

oracle - PlSql Translate函数问题

java - 在 Hibernate 4.2.21 中删除表的所有行

regex - 使用 bash 计算模式之间的行数/字符数

java - 使用 Java 替换来自阿拉伯语推文的表情符号 Unicode 范围

sql - 比较两个分隔字符串并在 PL SQL 中返回相应的值

java - 日期格式图片在转换整个输入字符串之前结束

sql - 在 Oracle 中将时差转换为给定格式