REGEXP_REPLACE如何遍历文本字符串的变量出现

时间:2015-01-01 02:35:23

标签: regex plsql oracle11g regexp-replace

我正在使用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

1 个答案:

答案 0 :(得分:0)

如果您现在还没有弄明白,这应该会有所帮助。基本上,正则表达式匹配一个字符串,该字符串由任何字符组成(非贪婪)并包括字符串&#34; base64逗号&#34;,后面跟着一个不是双引号的下一个字符串的记忆组,然后是双引号,并返回该匹配的记忆组部分,每次出现一次字符串&#34; base64&#34;在原始字符串中:

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.

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