【问题标题】:Regex to remove HTML Tags, empty lines and blank spaces in sql query正则表达式删除 sql 查询中的 HTML 标签、空行和空格
【发布时间】:2022-01-21 07:35:12
【问题描述】:

我有一个表,其中包含一个来自前端的免费文本列反馈。此列的值类似于 -

FEEDBACK
-Agent was listening and very attentive.

Agent showed all the houses and gave the right description

Agent was well versed & knew how to speak multiple
languages





 
-<p>Agent was well dressed for the event</p>

由于这是复制粘贴,因此后端有时会在两行之间有很多空格或空行。

我想删除所有这些并显示如下输出 -

FEEDBACK
-Agent was listening and very attentive.
Agent showed all the houses and gave the right description
Agent was well versed & knew how to speak multiple
languages
-Agent was well dressed for the event

为此,我使用以下查询 -

select REGEXP_REPLACE(regexp_replace(  regexp_replace(
    regexp_replace(
      DBMS_LOB.SUBSTR(max(feedback),4000),
      /*
        Replace LF followed by any non-printable sequence that ends with newline
        with single newline
      */
      chr(10) || '[^[:graph:]]*(' || chr(13) || '?' || chr(10) || ')',
      chr(10) || '\1'
    ),
    /*Then replace newline repetitions*/
    '(' || chr(13) || '?' || chr(10) || ')+',
    '\1'
  ),'<.*?>'),'&nbsp;') as feedback
  from dual;

有什么方法可以合并这些 regex_replace 而不是使用多个 regex_replace 来满足我的要求?

【问题讨论】:

  • 可能前端是更适合该处理的地方

标签: sql regex oracle


【解决方案1】:

并非所有都可以组合。
但有些可以,通过正则表达式或|

然后最好先替换那些。
因为删除它们可能会导致额外的空行。

SELECT
  REGEXP_REPLACE(
    REGEXP_REPLACE(
      REGEXP_REPLACE(DBMS_LOB.SUBSTR(feedback, 4000)
      , '(&nbsp;)|(<[/[:alpha:]]+>)')
      , '[[:space:]]+$','',1,0,'m') 
      , '(['||chr(13)||']?['||chr(10)||']){2,}','\1') AS feedback
FROM your_table
ORDER BY feedback DESC
OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY

【讨论】:

    猜你喜欢
    • 2021-04-10
    • 1970-01-01
    • 2016-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多