regular expressions for postgres syntax

Discussion in 'Perl Misc' started by Jan Biel, Apr 21, 2004.

  1. Jan Biel

    Jan Biel Guest

    Hi there!

    I'm looking for a regular expression or a set of expressions able to cut
    Postgres (or any SQL) code into its syntactical parts.

    For example if the code was

    CREATE TABLE table_a (
    attrib_a varchar(8) NOT NULL,
    attrib_b varchar(10)
    );

    I need to extract the components like this:

    CREATE TABLE
    table_a
    ---
    attib_a
    varchar(8)
    NOT NULL
    ---
    attrib_b
    varchar(10)
    ---

    This is only a simplified example, since SQL code can be much more complex.
    I was hoping there are given regular expressions so I wouldn't have to do
    all the work by myself (which I'm not sure I'm able to).

    Postgres would be awesome, but any SQL version would do since I could
    translate the expressions rather easily, I guess.

    Thanks in advance on any help towards the matter
    Janbiel
    Jan Biel, Apr 21, 2004
    #1
    1. Advertising

  2. Jan Biel

    Jack Challen Guest

    Jan Biel wrote:
    > I'm looking for a regular expression or a set of expressions able to cut
    > Postgres (or any SQL) code into its syntactical parts.


    You really *don't* want to try a regex approach. SQL's an entire language,
    I doubt a regular expression would be possible, and it certainly wouldn't
    be readable.

    You want a parser.
    In fact, you want SQL::parser.

    http://search.cpan.org/search?query=sql::parser&mode=module

    jack
    Jack Challen, Apr 21, 2004
    #2
    1. Advertising

  3. Jan Biel

    Jack Challen Guest

    Jan Biel wrote:
    > I'm looking for a regular expression or a set of expressions able to cut
    > Postgres (or any SQL) code into its syntactical parts.


    Whoops. Previous post missed out obvious module to try...

    You really *don't* want to try a regex approach. SQL's an entire language,
    I doubt a regular expression would be possible, and it certainly wouldn't
    be readable.

    You want a parser.
    In fact, you want SQL::parser, or SQL::Translator::parser::postgreSQL

    http://search.cpan.org/search?query=sql::parser&mode=module
    http://search.cpan.org/~kclark/SQL-Translator-0.05/lib/SQL/Translator/Parser/PostgreSQL.pm
    -or-
    http://tinyurl.com/yruzd

    jack
    Jack Challen, Apr 21, 2004
    #3
  4. Jan Biel wrote:

    > Hi there!
    >
    > I'm looking for a regular expression or a set of expressions able to cut
    > Postgres (or any SQL) code into its syntactical parts.


    You want a parser, not a regular expression. If you dont know the difference
    then you are out of your depth.

    gtoomey
    Gregory Toomey, Apr 21, 2004
    #4
  5. Jan Biel

    Jan Biel Guest

    Jack Challen wrote:

    > You want a parser.
    > In fact, you want SQL::parser, or SQL::Translator::parser::postgreSQL
    >
    > http://search.cpan.org/search?query=sql::parser&mode=module
    >

    http://search.cpan.org/~kclark/SQL-Translator-0.05/lib/SQL/Translator/Parser/PostgreSQL.pm
    > -or-
    > http://tinyurl.com/yruzd


    Thanks a bunch, exactly what I need (I guess). Didn't know there were
    parsers available just like that, like standalone parsers outside of
    compilers.

    Yes, I still have a lot to learn :)

    Thanks anyway,
    Janbiel
    Jan Biel, Apr 22, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jay Douglas

    Custom Regular Expressions in ASP.net

    Jay Douglas, Nov 2, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    600
    mikeb
    Nov 3, 2003
  2. Jay Douglas
    Replies:
    0
    Views:
    593
    Jay Douglas
    Aug 15, 2003
  3. Detlef Jockheck

    syntax-check with regular expressions?

    Detlef Jockheck, Jul 27, 2004, in forum: Python
    Replies:
    5
    Views:
    409
    Detlef Jockheck
    Aug 4, 2004
  4. Nick Black
    Replies:
    1
    Views:
    146
    Kashia Buch
    Nov 16, 2006
  5. Noman Shapiro
    Replies:
    0
    Views:
    222
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page