Regular expression to remove comments from SQL statement

后端 未结 8 1772
时光说笑
时光说笑 2020-12-15 12:51

I\'m trying to come up with a regular expression to remove comments from an SQL statement.

This regex almost works:

(/\\*([^*]|[\\r\\n]|(\\*+([^*/]|         


        
8条回答
  •  一生所求
    2020-12-15 13:06

    For all PHP folks: please use this library - https://github.com/jdorn/sql-formatter. I have been dealing with stripping comments from SQL for couple years now and the only valid solution would be a tokenizer/state machine, which I lazily resisted to write. Couple days ago I found out this lib and ran 120k queries through it and found only one bug (https://github.com/jdorn/sql-formatter/issues/93), which is fixed immediately in our fork https://github.com/keboola/sql-formatter.

    The usage is simple

    $query <<

提交回复
热议问题