Parsing SQL string

前端 未结 5 1083
我寻月下人不归
我寻月下人不归 2021-01-21 08:44

What is a good method for parsing an sql string into it separate components. I\'v tried with a regex, however I can\'t get it to work just right.

Say for instance:

5条回答
  •  滥情空心
    2021-01-21 09:26

    A regular expression can parse only a regular language. That is, a language that can be expressed with a finite state machine.

    Most programming languages, including SQL, cannot be expressed as a regular language, and thus cannot be parsed with regular expressions. Even doing limited tasks like splitting SQL expressions is going to be maddeningly difficult with regular expressions.

    You'll need to use a more robust parser generator that can handle more complex grammars. You tag your question with PHP, so you might want to look into Lime.

    You'll also need to find a grammar file for PHP compatible with the parser generator you choose. I'm not sure where to get one for PHP and Lime, but there's a pure Perl project called DBIx::MyParsePP that is based on MySQL's grammar.

提交回复
热议问题