Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

Regex untuk mencocokkan komentar MySQL

Di PHP, saya menggunakan kode ini untuk menghapus komentar SQL:

$sqlComments = '@(([\'"`]).*?[^\\\]\2)|((?:\#|--).*?$|/\*(?:[^/*]|/(?!\*)|\*(?!/)|(?R))*\*\/)\s*|(?<=;)\[email protected]';
/* Commented version
$sqlComments = '@
    (([\'"`]).*?[^\\\]\2) # $1 : Skip single & double quoted + backticked expressions
    |(                   # $3 : Match comments
        (?:\#|--).*?$    # - Single line comments
        |                # - Multi line (nested) comments
         /\*             #   . comment open marker
            (?: [^/*]    #   . non comment-marker characters
                |/(?!\*) #   . ! not a comment open
                |\*(?!/) #   . ! not a comment close
                |(?R)    #   . recursive case
            )*           #   . repeat eventually
        \*\/             #   . comment close marker
    )\s*                 # Trim after comments
    |(?<=;)\s+           # Trim after semi-colon
    @msx';
*/
$uncommentedSQL = trim( preg_replace( $sqlComments, '$1', $sql ) );
preg_match_all( $sqlComments, $sql, $comments );
$extractedComments = array_filter( $comments[ 3 ] );
var_dump( $uncommentedSQL, $extractedComments );


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. laravel mencari beberapa kata yang dipisahkan oleh spasi

  2. MySQL INSERT ....ON DUPLICATE UPDATE - Menambahkan satu ke peningkatan otomatis

  3. Ruby:mysql2-Gem tidak berfungsi (Mac OS X Snow Leopard, Ruby 1.9.2)

  4. Bagaimana cara men-debug Kueri MySQL/Doctrine2?

  5. REPLACE case-insensitive di MySQL?