2

我有一个 Java 字符串,它实际上是一个 SQL 脚本。

CREATE OR REPLACE PROCEDURE Proc
   AS
        b NUMBER:=3;
        c VARCHAR2(2000);
    begin
        c := 'BEGIN ' || ' :1 := :1 + :2; ' || 'END;';
   end Proc;

我想用分号拆分脚本,除了那些出现在字符串中的脚本。所需的输出是四个不同的字符串,如下所述

1- CREATE OR REPLACE PROCEDURE Proc AS b NUMBER:=3
2- c VARCHAR2(2000)
3- begin c := 'BEGIN ' || ' :1 := :1 + :2; ' || 'END;';
4- end Proc

Java Split() 方法也会将上面的字符串拆分为标记。我想保留这个字符串,因为分号在引号内。

c := 'BEGIN ' || ' :1 := :1 + :2; ' || 'END;';

Java Split() 方法输出

1- c := 'BEGIN ' || ' :1 := :1 + :2
2- ' || 'END
3- '

请建议一个正则表达式,它可以在分号上分割字符串,除了那些在字符串中的字符串。

===================== CASE-2 ========================

以上部分已回答及其工作

这是另一个更复杂的案例

==================================================== ====

我有一个 SQL 脚本,我想标记每个 SQL 查询。每个 SQL 查询由分号 (;) 或正斜杠 (/) 分隔。

1-如果它们出现在类似的字符串中,我想转义分号或 / 符号

...WHERE col1 = 'some ; name/' ..

2- 表达式还必须转义任何多行注释语法,即 /*

这是输入

/*Query 1*/
SELECT
*
FROM  tab t
WHERE (t.col1 in (1, 3)
            and t.col2 IN (1,5,8,9,10,11,20,21,
                                     22,23,24,/*Reaffirmed*/
                                     25,26,27,28,29,30,
                                     35,/*carnival*/
                                     75,76,77,78,79,
                                     80,81,82, /*Damark accounts*/
                                     84,85,87,88,90))
;
/*Query 2*/    
select * from table
/
/*Query 3*/
select col form tab2
;
/*Query 4*/
select col2 from tab3 /*this is a multi line comment*/
/

期望的结果

[1]: /*Query 1*/
    SELECT
    *
    FROM  tab t
    WHERE (t.col1 in (1, 3)
                and t.col2 IN (1,5,8,9,10,11,20,21,
                                         22,23,24,/*Reaffirmed*/
                                         25,26,27,28,29,30,
                                         35,/*carnival*/
                                         75,76,77,78,79,
                                         80,81,82, /*Damark accounts*/
                                         84,85,87,88,90))

[2]:/*Query 2*/    
    select * from table

[3]: /*Query 3*/
    select col form tab2

[4]:/*Query 4*/
    select col2 from tab3 /*this is a multi line comment*/

其中一半已经可以通过上一篇文章中向我提出的建议来实现(link a start),但是当在查询中引入注释语法(/*)并且每个查询也可以用正斜杠(/)分隔时,表达式不起作用。

4

3 回答 3

4

正则表达式模式((?:(?:'[^']*')|[^;])*);应该可以满足您的需求。使用while循环并Matcher.find()提取所有 SQL 语句。就像是:

Pattern p = Pattern.compile("((?:(?:'[^']*')|[^;])*);";);
Matcher m = p.matcher(s);
int cnt = 0;
while (m.find()) {
    System.out.println(++cnt + ": " + m.group(1));
}

使用您提供的示例 SQL,将输出:

1: CREATE OR REPLACE PROCEDURE Proc
   AS
        b NUMBER:=3
2: 
        c VARCHAR2(2000)
3: 
    begin
        c := 'BEGIN ' || ' :1 := :1 + :2; ' || 'END;'
4: 
   end Proc

如果您想获得终止;,请使用m.group(0)而不是m.group(1).

有关正则表达式的更多信息,请参阅Pattern JavaDoc 和这个很好的参考。以下是该模式的概要:

(              Start capturing group
  (?:          Start non-capturing group
    (?:        Start non-capturing group
      '        Match the literal character '
      [^']     Match a single character that is not '
      *        Greedily match the previous atom zero or more times
      '        Match the literal character '
    )          End non-capturing group
    |          Match either the previous or the next atom
    [^;]       Match a single character that is not ;
  )            End non-capturing group
  *            Greedily match the previous atom zero or more times
)              End capturing group
;              Match the literal character ;
于 2011-09-15T18:36:12.337 回答
0

您可以尝试的只是拆分“;”。然后对于每个字符串,如果它有奇数个 's,则将其与以下字符串连接,直到它有偶数个 's 添加“;”s。

于 2011-09-15T12:17:43.400 回答
0

我遇到了同样的问题。我看到了以前的建议,并决定改进以下方面的处理:

  • 评论
  • 转义单引号
  • 不以分号结尾的单个查询

我的解决方案是为 java 编写的。反斜杠转义和 DOTALL 模式等某些事情可能会从一种语言更改为另一种语言。

这对我有用"(?s)\s*((?:'(?:\\.|[^\\']|''|)'|/\.*?\*/|(?:--|#)[^\r\n]|[^\\'])?)(?:;|$)"

"
(?s)                 DOTALL mode. Means the dot includes \r\n
\\s*                 Initial whitespace
(
    (?:              Grouping content of a valid query
        '            Open string literal
        (?:          Grouping content of a string literal expression
            \\\\.    Any escaped character. Doesn't matter if it's a single quote
        |
            [^\\\\'] Any character which isn't escaped. Escaping is covered above.
        |
            ''       Escaped single quote
        )            Any of these regexps are valid in a string literal.
        *            The string can be empty 
        '            Close string literal
    |
        /\\*         C-style comment start
        .*?          Any characters, but as few as possible (doesn't include */)
        \\*/         C-style comment end
    |
        (?:--|#)     SQL comment start
        [^\r\n]*     One line comment which ends with a newline
    |
        [^\\\\']     Anything which doesn't have to do with a string literal
    )                Theses four tokens basically define the contents of a query
    *?               Avoid greediness of above tokens to match the end of a query
)
(?:;|$)              After a series of query tokens, find ; or EOT
"

至于您的第二种情况,请注意正则表达式的最后一部分表示您的正则表达式将如何结束。现在它只接受分号文本结尾。但是,您可以在结尾添加任何您想要的内容。例如(?:;|@|/|$)接受at斜杠作为结束字符。尚未为您测试此解决方案,但应该不难。

于 2013-05-19T18:22:34.257 回答