PL/SQL-REGEXP_LIKE
我正在编写一个过程,该过程从存储库中读取一些 PL/SQL 代码,然后验证每个过程/函数是否具有适当的文档标头。给出变量 my_code 中包含的以下 PL/SQL 代码:
my_code varchar2(1024) := '
script package test_lib
is
-------------------------------------------------------------------------------
-- <function name="my_func" begin="9-Dec-2010">
-- <summary>
-- Test function.
-- </summary>
-- <authors>
-- <author name="Giuseppe Greco" email="[email protected]"/>
-- </authors>
-- <params>
-- <param name="num" mandatory="yes" type="input">
-- Test param.
-- </param>
-- </params>
-- <return>
-- True if it is an alarm; otherwise, false.
-- </return>
-- </function>
--------------------------------------------------------------------------------
function my_func(par1 NUMBER)
return boolean
is
l_num NUMBER := 0;
begin
if l_num < 1 then
dbms_output.put_line(''my_func'');
end if;
return true;
end my_func;
end test_lib;
';
我尝试过类似的操作...但它不起作用:
if REGEXP_LIKE(my_code, 'function (\w).+end \1;') then
l_number_of_funcs := LENGTH(
REGEXP_REPLACE(
my_code,
'([^f]+[^u]+[^n]+[^c]+[^t]+[^i]+[^o]+[^n]+)(.+)is(.*)begin(.*)end([a-z0-9_\-]+);'));
end if;
在上面的语句中,REGEXP_LIKE 永远不会返回 true,因此 REGEXP_REPLACE 永远不会执行。我正在做的是确定代码是否至少包含一个过程,如果包含,我调用 REGEXP_REPLACE 来确定代码包含多少个过程(在上面的示例中,它应该返回 1)。我首先调用 REGEXP_LIKE,因为如果代码不包含任何过程,REGEXP_REPLACE 就会崩溃。
有什么想法或建议吗?任何帮助将不胜感激。
谢谢, 杰夫
I'm writing a procedure that reads some PL/SQL code from a repository and then verifies that each procedure/function has the appropriate documentation header. Giving the following PL/SQL code contained in variable my_code:
my_code varchar2(1024) := '
script package test_lib
is
-------------------------------------------------------------------------------
-- <function name="my_func" begin="9-Dec-2010">
-- <summary>
-- Test function.
-- </summary>
-- <authors>
-- <author name="Giuseppe Greco" email="[email protected]"/>
-- </authors>
-- <params>
-- <param name="num" mandatory="yes" type="input">
-- Test param.
-- </param>
-- </params>
-- <return>
-- True if it is an alarm; otherwise, false.
-- </return>
-- </function>
--------------------------------------------------------------------------------
function my_func(par1 NUMBER)
return boolean
is
l_num NUMBER := 0;
begin
if l_num < 1 then
dbms_output.put_line(''my_func'');
end if;
return true;
end my_func;
end test_lib;
';
I've tried something similar... but it does not work:
if REGEXP_LIKE(my_code, 'function (\w).+end \1;') then
l_number_of_funcs := LENGTH(
REGEXP_REPLACE(
my_code,
'([^f]+[^u]+[^n]+[^c]+[^t]+[^i]+[^o]+[^n]+)(.+)is(.*)begin(.*)end([a-z0-9_\-]+);'));
end if;
In the statements above, REGEXP_LIKE never returns true, so REGEXP_REPLACE is never executed. What I'm doing is determining whether or not the code contains at least one procedure, and if it does, I invoke REGEXP_REPLACE to determine how many procedures the code contains (in the example above it should return 1). I first invoke REGEXP_LIKE because if the code does not contain any procedure, REGEXP_REPLACE crashes.
Any idea or suggestion? Any help would be really appreciated.
Thanks,
Jeff
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试更简单的正则表达式
REGEXP_COUNT 函数可能更适合这种情况。
多行的存在会严重破坏这一点。如果有多行,即使上面 (1) 中的简化正则表达式也不起作用。我建议您编写一个注释剥离器和换行符删除器,将代码字符串传递给这些函数,然后通过正则表达式运行它。
无论如何,使用正则表达式来解析文本通常并不像希望的那样有用。您确实需要一个 PL/SQL 解析器 - 请参阅
http://database-geek.com/2009/02/06/building-a-plsql-code-parser-using-plsql-part-1-2/
分享并享受。
Try a simpler regular expression
The REGEXP_COUNT function might be more appropriate in this context.
The presence of multiple lines is going to break this very badly. Even the simplified regular expression in (1) above doesn't work if there are multiple lines. I suggest that you write a comment-stripper and newline-remover, pass your code string through those functions, then run it through the regular expression.
In any case, using regular expressions to parse text is often-times not as helpful as might be hoped. You really need a parser for PL/SQL - see
http://database-geek.com/2009/02/06/building-a-plsql-code-parser-using-plsql-part-1-2/
Share and enjoy.
我认为使用 NVL 您不需要两次检查。尝试:
我无法对此进行测试,但它应该朝着正确的方向发展。
I think with NVL you do not need two checks. Try:
I could not test this, but it should go in the right direction.