简体   繁体   English

SQL注释中的Java Regex查找/替换模式

[英]Java Regex find/replace pattern in SQL comments

I want to find/replace a character/pattern ONLY inside SQL comments ( single line comments -- and block comments /* */). 我只想在SQL注释(单行注释-和块注释/ * * /)中查找/替换字符/模式。 The source string is an SQL script. 源字符串是一个SQL脚本。

At the moment I am searching for a semi-colon (;) within comments and want to replace it with blank space. 目前,我正在注释中搜索分号(;),并希望将其替换为空格。

Source 资源

CREATE OR REPLACE PROCEDURE TESTDBA.PROC_REUSING_BINDED_VAR_N_DSQL

 AS
        a NUMBER:=2;
        b NUMBER:=3; -- jladjfljaf; lakjflajf
-- alksdjflkjaf ladkjf 
        v_plsql_tx VARCHAR2(2000);
    begin
        v_plsql_tx := 'BEGIN ' || ' :1 := :1 + :2; ' || 'END;';
        execute immediate v_plsql_tx
        using in out a, b;
        insert into testdba.NEW_TESTING_TABLE(CHARACTER_VALUE) VALUES('a='||a);
   end PROC_REUSING_BINDED_VAR_N_DSQL;
-- lajdflajf

/*lakjdfljalfdk; alkdjf*/

/*asdf 
;
asdfa*/

/*
    adf
asd asdf


*/

Can you please suggest something. 你能建议点什么吗?

Thanks 谢谢

I would do this like this : 我会这样做:

    try {
    Pattern regex = Pattern.compile("(?:/\\*[^;]*?\\*/)|(?:--[^;]*?$)", Pattern.DOTALL | Pattern.MULTILINE);
    Matcher regexMatcher = regex.matcher(subjectString);
    while (regexMatcher.find()) {
        // matched text: regexMatcher.group()
        // match start: regexMatcher.start()
        // match end: regexMatcher.end()
    } 
} catch (PatternSyntaxException ex) {
    // Syntax error in the regular expression
}

The above will give you all comments without ';'. 以上将为您提供所有不带';'的注释。 Then I would iterate line by line through the sql file and when I encountered a line which had a comment I would check to see if that line is in my list of matches - if not then I would search replace ; 然后我将逐行遍历sql文件,当我遇到一行带有注释的行时,我将检查该行是否在我的匹配项列表中-如果没有,则将搜索replace; with ' ' in the whole comment. 整个注释中带有“”。 Of course you will have to find where the comment ends but this is easy -- ends in the same line and /* and when the first */ is found. 当然,您将必须找到注释的结尾位置,但这很容易-在同一行和/ *处以及在找到第一个* /时结束。 This way you can change any number of ; 这样,您可以更改任何数量的; with the same code. 具有相同的代码。

Probably the best bet is to use two regexes and two Patterns (one single line and one multi-line). 最好的选择是使用两个正则表达式和两个模式(一个单行和一个多行)。

Single Line: \\\\-\\\\-[^;]*(;) -- not sure the best way to find multiple ; 单行: \\\\-\\\\-[^;]*(;) -不确定找到多个的最佳方法; within a line 在一行内

Multi-line: /\\\\*[^;(\\\\*/)]*?(;)[^;]*?\\\\*/ -- something like this anyway 多行: /\\\\*[^;(\\\\*/)]*?(;)[^;]*?\\\\*/ -无论如何

What you need to find out first: There are two ways that multi-line comments can be handled. 首先需要了解的内容:处理多行注释的方式有两种。

  1. A single "*/" closes all currently open "/*". 单个“ * /”关闭所有当前打开的“ / *”。
  2. For every "/*" your need a corresponding "*/" (nested comments). 对于每个“ / *”,您都需要一个对应的“ * /”(嵌套注释)。

The first one is relatively easy to implement. 第一个相对容易实现。 The second can only be done by either deep magic regex (read: unmaintainable by future coders) or with a short program. 第二种方法只能通过深度魔术正则表达式(阅读:以后的编码人员无法维护)或使用简短的程序来完成。

The first one is pretty easy: Using "/\\*.*;.*\\*/" will give you a match whenever there is an embedded semicolon. 第一个很简单:只要有嵌入式分号,使用“ /\\*.*;..*\\*/”就会为您提供匹配。

The second one will need a bit of programming. 第二个将需要一些编程。 If you encounter a ";", you will need to check if you're currently inside a comment. 如果遇到“;”,则需要检查当前是否在注释中。 You can know by just sequentially reading the file (ignoring the carriage return/line feeds) and incrementing a number whenever you encounter a "/*" and decrement the number when encountering a "*/". 您可以通过顺序读取文件(忽略回车/换行)并在遇到“ / *”时增加一个数字,而在遇到“ * /”时减少一个数字来知道。 If the number is at least 1, your semicolon is inside a comment. 如果数字至少为1,则分号位于注释中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM