【问题标题】:REGEXP_REPLACE How to loop through variable occurrences of a text stringREGEXP_REPLACE 如何循环遍历文本字符串的变量出现
【发布时间】:2015-02-27 22:40:00
【问题描述】:

我正在使用 Oracle 11.g 程序。我发现这个REGEXP_REPLACE 的例子只有两个参数(输入和模式)。它有效,但不是最佳的。我正在尝试使用REGEXP_REPLACE 来循环遍历出现在文本 base64, 之后和文本 " /> 之前的特定文本字符串的可变次数强> 我可以让它工作一次,但我不能让它正常循环。

Declare p_html    clob;
        l_image_clob  clob;
        l_image_count number;
Begin
p_html := '<p>Some header text base64,one start here and then this is the end one" /></p><p>Some header text base64,two start here and then this is the end two" /></p>';
l_image_count := REGEXP_COUNT(p_html, 'base64', 1, 'i');
If l_image_count > 0 Then
  For i In 1..l_image_count Loop
    l_image_clob := REGEXP_REPLACE(p_html, '(.*base64,)|(" />.*)');
    dbms_output.put_line(l_image_clob);
    -- code to process each occurrence individually.
  End Loop;
End If;
End;

我希望看到的数据结果是:

one start here and this is the end one 
two start here and this is the end two

上面的例子返回:

two start here and this is the end two
two start here and this is the end two

我尝试了几个选项 REXEXP_REPLACE,但我似乎无法使其与变量 i 一起使用。

【问题讨论】:

  • REGEXP_REPLACE() 不需要 3 个参数吗?要搜索的字符串、正则表达式和正则表达式的替换。
  • @Barmar 否,使用 2 个参数删除匹配的字符串 documentation

标签: regex plsql oracle11g regexp-replace


【解决方案1】:

如果您现在还没有弄清楚,这应该会有所帮助。基本上,正则表达式匹配一个字符串,该字符串由最多(非贪婪)的任何字符组成,包括字符串“base64 comma”,然后是记住的下一个不是双引号的字符串组,然后是双引号引用并返回匹配的记忆组部分,对于原始字符串中每次出现的字符串“base64”一次:

with tbl(p_html) as
( select '<p>Some header text base64,one start here and then this is the end one" /></p><p>Some header text base64,two start here and then this is the end two" /></p>'
  from dual
)
select REGEXP_substr(p_html, '.*?base64,([^"]*)"', 1, level, NULL, 1) text
from tbl
connect by level <= REGEXP_COUNT(p_html, 'base64', 1, 'i');


TEXT                                                                            
--------------------------------------------------------------------------------
one start here and then this is the end one                                     
two start here and then this is the end two                                     

2 rows selected.

将其包装在游标中进行循环,或者根据需要将其加载到集合中进行处理。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-24
    • 1970-01-01
    • 1970-01-01
    • 2014-09-19
    相关资源
    最近更新 更多