Regular expression to remove comments from SQL statement

后端 未结 8 1709
时光说笑
时光说笑 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

    As you said that the rest of your regex is fine, I focused on the last part. All you need to do is verify that the -- is at the beginning and then make sure it removes all dashes if there are more than 2. The end regex is below

    (^[--]+)
    

    The above is just if you want to remove the comment dashes and not the whole line. You can run the below if you do want everything after it to the end of the line, also

    (^--.*)
    

提交回复
热议问题