是否可以使用函数进行 regexp_replace ?
我想对字符串中的值进行一些计算,最后替换它们。 Oracle 正则表达式似乎很好,但 \1 在所有计算结束时都会被评估。所以我想知道我是否可以在将其传递给函数之前进行评估?
set serveroutput on
declare
l_foo varchar2(4000);
function f_test(i_t varchar2) return varchar2
is
begin
dbms_output.put_line('given parameter: ' || i_t);
return upper(i_t);
end;
begin
l_foo := regexp_replace(
'http://www.scoach.com/${asset_type}/${ISIN}?eventtarget=${target}ANDeventvalue=${target_value}'
,'\$\{([[:alpha:]_]+)\}'
,f_test('\1')
);
dbms_output.put_line(l_foo);
end;
给你一个像这样的结果:
given parameter: \1
http://www.scoach.com/asset_type/ISIN?eventtarget=targetANDeventvalue=target_value
PL/SQL procedure successfully completed.
I would like to do some calculations on a value in a string and finally replace them. Oracles regexp seemes to be good but the \1 gets evaluated at the end of all calcualtions. So I am wondering if I could fore the evaluation before passing it to the function?
set serveroutput on
declare
l_foo varchar2(4000);
function f_test(i_t varchar2) return varchar2
is
begin
dbms_output.put_line('given parameter: ' || i_t);
return upper(i_t);
end;
begin
l_foo := regexp_replace(
'http://www.scoach.com/${asset_type}/${ISIN}?eventtarget=${target}ANDeventvalue=${target_value}'
,'\$\{([[:alpha:]_]+)\}'
,f_test('\1')
);
dbms_output.put_line(l_foo);
end;
Gives you a result like:
given parameter: \1
http://www.scoach.com/asset_type/ISIN?eventtarget=targetANDeventvalue=target_value
PL/SQL procedure successfully completed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来将反向引用传递到 reg ex 函数中的函数中是行不通的(至少在我的测试中,并且找不到任何可以正常工作的东西(尽管有这个 link 但很难称其为参考)
但是你可以这样做,但是处理会很慢,但是它应该可以工作。我基于这个链接
集 您可以看到我只是循环
遍历并将结果
放入 varchar2 表中,然后使用函数结果逐行进行替换(请注意)。可能是更有效的方法,我并不是在追求效率,而是只是为了让它发挥作用)
it looks like passing the backreference into a function within the reg ex function is not going to work (at least in my test and the lack of finding anything out there that works properly (although there was this link but it is hard to call this a reference)
But you can do this, but it'll be slow-by-slow processing but it ought to work. I based this sample from this link
set serveroutput on
which results in
This was done in 11gR1. You can see that I am just looping through this and placing the results into a varchar2 table and then doing the replacement line-by-line from there with the function results. (please note there are probably more efficient ways of doing this, I was not striving for efficiency but rather just to get it to work)