Get function and description comment in a pl/sql body package file

时间:2019-04-08 13:36:30

标签: regex plsql notepad++

I need to extract in Notepad++ from a package body file the object name (function, procedures, etc) and the description from the object.

I know that is possible by regex but sometimes the comments are compound for more than 3 lines.

CREATE OR REPLACE PACKAGE BODY pac_example AS

/* *********************************************
*  Function f1
*  Description: Search for a data in table1
*  (another comments)
*  ******************************************* */
FUNCTION f1 RETURN NUMBER IS
BEGIN
SELECT * FROM table1;
RETURN 1;
END f1;



/* *********************************************
*  Function f2
*  Description: Search for a data in table2
*  (another comments)
*  (another comments)
*  (another comments)
*  ******************************************* */
FUNCTION f2 RETURN NUMBER IS
BEGIN
  SELECT * FROM table2;
RETURN 1;
END f2;

END pac_example;

And in this case, I need only to replace all in document and get something like:

/* *********************************************
*  Function f1
*  Description: Search for a data in table1
*  (another comments)
*  ******************************************* */
FUNCTION f1 RETURN NUMBER IS
/* *********************************************
*  Function f2
*  Description: Search for a data in table2
*  (another comments)
*  (another comments)
*  (another comments)
*  ******************************************* */
FUNCTION f2 RETURN NUMBER IS

Or (best scenario) this:

FUNCTION f1 Search for a data in table1
FUNCTION f2 Search for a data in table2

2 个答案:

答案 0 :(得分:1)

  • Ctrl + H
  • 查找内容:(?:\A.*?|\G)\*\h+((?:Function|Procedure)\h*\w+).*?Description:\h*([^\r\n]+\R)(?:(?!\*\h+(?:Function|Procedure)).)+
  • 替换为:$1 $2
  • UNcheck区分大小写
  • 检查环绕
  • 检查正则表达式
  • 检查. matches newline
  • 全部替换

说明:

(?:                         # non capture group
    \A                      # beginning of file
    .*?                     # 0 or more any character
  |                         # OR
    \G                      # restart from last match position
)                           # end group
\*                          # an asterisk
\h+                         # 1 or more horizontal spaces
(                           # start group 1
    (?:Function|Procedure)  # literally Function OR Procedure
    \h+                     # 1 or more horizontal spaces
    \w+                     # 1 or more word character
)                           # end group 1
.*?                         # 1 or more any character, not greedy
Description:\h*             # literally Description followed by horizontal spaces
(                           # start group 2
    [^\r\n]+                # 1 or more any character not linebreak
    \R                      # any kind of linebreak
)                           # end group 2
(?:                         # Tempered greedy token
    (?!                     # negative lookahead
        \*                  # an asterisk
        \h+                 # 1 or more horizontal spaces
        (?:Function|Procedure)  # literally Function OR Procedure
    )                       # end lookahead
    .                       # any character
)+                          # end group, appears 1 or more times

替换:

$1          # content of group 1, function or procedure
$2          # content of group 2, description

屏幕截图:

enter image description here

答案 1 :(得分:0)

To do this in Notepad++ I would:

  1. Regex for ^([ \t\*]+)(FUNCTION|PROCEDURE)([ \t]+)([A-Z_0-9]+). Choose on the Mark tab and click Mark all.
  2. Now search for ^([ \t\*]+)Description: (.+). Again click Mark all.
  3. Menu Search > Bookmark > Copy Bookmarked Lines
  4. Paste into a new document.

This will give you the basis for best scenario. It's a simple matter to tidy the lines from there using regex replace.

Notes:

  • The first step selects the procedure name from the comments i.e. * Function f2. You close the capitalisation but at least it's in the right order. Otherwise you'd need to switch the procedure name and description for every line.
  • This is no good if you need signature details (arguments, etc). Then you'll need to search for ^([ \t]*)(FUNCTION|PROCEDURE)([ \t]+)([A-Z_0-9]+) and re-order the descriptions
  • I've included tab and space patterns, because it's not clear how your source is indented