Java 如何更改字符串的格式/模式?

Java 如何更改字符串的格式/模式?,java,sql,regex,eclipse,Java,Sql,Regex,Eclipse,我有一个类似下面的字符串,我想写一些东西来检查以下格式 Insert into TABLE(A, B, C, D, E, F, G, H) values (123, 'VALUE ' , ' ', ' ', 'XXX', 'CCC', ' ', ' '); 这将是一个错误 Insert into TABLE(A, B, C, D, E, F, G, H) values (123, ''VALUE ''' , '', '', 'RED', 'FAX', '', ''); 正如你所看到的,第

我有一个类似下面的字符串,我想写一些东西来检查以下格式

Insert into TABLE(A, B, C, D, E, F, G, H) values (123, 'VALUE ' , ' ', ' ', 'XXX', 'CCC', ' ',  ' ');
这将是一个错误

Insert into TABLE(A, B, C, D, E, F, G, H) values (123, ''VALUE ''' , '', '', 'RED', 'FAX', '',  '');
正如你所看到的,第二个有一个额外的逗号或额外的引号。 (基本上检查值括号内的所有内容,插入内容不会更改。)

我想检查错误的模式,并编辑它的飞行。有什么想法吗?

描述

此正则表达式将执行以下操作:

  • 查找以类似于插入表(..)值的结构开头的行
  • 验证
    部分是否包含逗号分隔的值列表,这些值仅为
    • 未用引号括起的字符串,如
      123
    • 用单引号括起来的字符串,如
      'red'
  • 允许空白环绕任何分隔逗号
  • 允许带引号的字符串包含逗号,如
    “此值有逗号”
注意事项:

  • 如果正则表达式与您的字符串不匹配,则源字符串有问题
  • 我建议对此使用以下标志:Case instive
例子 现场示例

在本例中,我使用了多行、全局和忽略空白选项。以更好地说明其功能

源字符串

这里的最后一行无效

Insert into TABLE(A, B, C, D, E, F, G, H) values (123);
Insert into TABLE(A, B, C, D, E, F, G, H) values (123, 'VALUE ', ' ', ' ', 'XXX', 'CCC', ' ', ' ');
Insert into TABLE(A, B, C, D, E, F, G, H) values (123, 'VAL,UE ' , ' ', ' ', 'XXX', 'CCC', ' ',  ' ');
Insert into TABLE(A, B, C, D, E, F, G, H) values (123, ''VALUE ''' , '', '', 'RED', 'FAX', '',  '')
有效匹配项

Insert into TABLE(A, B, C, D, E, F, G, H) values (123)
Insert into TABLE(A, B, C, D, E, F, G, H) values (123, 'VALUE ', ' ', ' ', 'XXX', 'CCC', ' ', ' ')
Insert into TABLE(A, B, C, D, E, F, G, H) values (123, 'VAL,UE ' , ' ', ' ', 'XXX', 'CCC', ' ',  ' ')
解释
您是否允许用户以纯文本形式输入SQL?因为那是不,不!如果它是相同的SQL,就用“值”拆分字符串,用split[1]和逗号拆分字符串,然后计算每次拆分中撇号的数量。idk如果对你有用,但如果你只需要检查简单的情况,可能需要一个正则表达式,如
“Insert into TABLE\\(a,B,C,D,E,F,G,H\\)值\\(\\D+,“[^]”+“,”,“,”[^']+”,“[^']+”,“,”,“,”,”,“
够了吗?(它不会让正确转义的
通过)如果有更多的分数给你,我会的。完美的解释。非常感谢。
Insert into TABLE(A, B, C, D, E, F, G, H) values (123)
Insert into TABLE(A, B, C, D, E, F, G, H) values (123, 'VALUE ', ' ', ' ', 'XXX', 'CCC', ' ', ' ')
Insert into TABLE(A, B, C, D, E, F, G, H) values (123, 'VAL,UE ' , ' ', ' ', 'XXX', 'CCC', ' ',  ' ')
NODE                     EXPLANATION
----------------------------------------------------------------------
  ^                        the beginning of a "line"
----------------------------------------------------------------------
  [^(]*                    any character except: '(' (0 or more times
                           (matching the most amount possible))
----------------------------------------------------------------------
  table                    'table'
----------------------------------------------------------------------
  \(                       '('
----------------------------------------------------------------------
  [^)]*                    any character except: ')' (0 or more times
                           (matching the most amount possible))
----------------------------------------------------------------------
  \)                       ')'
----------------------------------------------------------------------
  [^(]*?                   any character except: '(' (0 or more times
                           (matching the least amount possible))
----------------------------------------------------------------------
  Values                   'Values'
----------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    \(                       '('
----------------------------------------------------------------------
    (?:                      group, but do not capture (0 or more
                             times (matching the most amount
                             possible)):
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        [^';\n\r]*               any character except: ''', ';', '\n'
                                 (newline), '\r' (carriage return) (0
                                 or more times (matching the most
                                 amount possible))
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        '                        '\''
----------------------------------------------------------------------
        [^']*                    any character except: ''' (0 or more
                                 times (matching the most amount
                                 possible))
----------------------------------------------------------------------
        '                        '\''
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
      \s*                      whitespace (\n, \r, \t, \f, and " ")
                               (0 or more times (matching the most
                               amount possible))
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        (?=                      look ahead to see if there is:
----------------------------------------------------------------------
          \)                       ')'
----------------------------------------------------------------------
        )                        end of look-ahead
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        ,                        ','
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
      \s*                      whitespace (\n, \r, \t, \f, and " ")
                               (0 or more times (matching the most
                               amount possible))
----------------------------------------------------------------------
    )*                       end of grouping
----------------------------------------------------------------------
    \)                       ')'
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------