【发布时间】:2015-04-29 12:23:31
【问题描述】:
我有以下 HTML 文档片段:
<ol>
<li>some text</li>
<li>some <strong>more</strong> text</li>
<li>some more text</li>
</ol>
<ul>
<li>even more text</li>
<li>...</li>
</ul>
我想要实现的是用##li## 和</li> 替换出现在<ol> 和</ol> 之间的所有<li> 实例##/li## 而<li> 和</li> 的所有实例<ul> 和 </ul> 之间应该保持不变:
<ol>
##li##some text##/li##
##li##some <strong>more</strong> text##/li##
##li##some more text##/li##
</ol>
<ul>
<li>even more text</li>
<li>...</li>
</ul>
虽然这主要是一个正则表达式问题,但如果我在存储过程中使用 Oracle XE 11g2 上的 Oracle REGEXP_REPLACE 函数感兴趣的话。
我很想发布到目前为止我尝试过的内容,但老实说,我完全迷失了这一点。
这个操作分两遍就可以了:
l_html_new :=
REGEXP_REPLACE(
l_html_old
, '<regex1 here>'
, '##li##'
);
l_html_new :=
REGEXP_REPLACE(
l_html_new
, '<regex2 here>'
, '##/li##'
);
更新:
@cfqueryparam,您的解决方案很有趣,因为它似乎完全符合我在 JS 中的需要。但是,我无法让它在 Oracle 中工作。 这是我所拥有的:
declare
--
c_crlf char(2) := chr(13)||chr(10);
--
l_html_old varchar2(4000);
l_html_new varchar2(4000);
l_pattern varchar2(400);
--
begin
l_html_old :=
'<ol>'||c_crlf
|| '<li>some text</li>'||c_crlf
|| '<li>some <strong>more</strong> text</li>'||c_crlf
|| '<li>some more text</li>'||c_crlf
|| '</ol>'||c_crlf
|| '<ul>'||c_crlf
|| '<li>even more text</li>'||c_crlf
|| '<li>...</li>'||c_crlf
|| '</ul>'
;
--
l_pattern := '<(li)>(.*?)<(\/li)>([^>]*)(?=(<li>.*?<\/li>[^>]*)*(?:[^>]*<\/ol>))';
--
l_html_new :=
REGEXP_REPLACE(
l_html_old --source_string
, l_pattern --pattern
, '##$1##$2##$3##$4' --replace_string
, 1 --position
, 0 --occurrence
, 'im' --match_parameter
);
--
dbms_output.put_line(l_html_new);
--
end;
这只是输出没有替换的原始字符串。 反向引用可能存在问题,但我认为这并不重要。由于根本没有发生替换,我认为没有任何匹配。
我会尝试找出JS和Oracle在处理上的区别。
【问题讨论】:
标签: sql regex oracle plsql tags