Parsing SQL Script with Regular Expression

Posted on 2005-04-13
Last Modified: 2008-02-01
Hi there,

Feel free to point me to the correct newsgroup if you can thik of a better one.

I would like to parse a SQL script using Regular Expressions

For example, I have this string in My SQL Script:

CREATE      VIEW      vw_Something     AS


I would like to grab the view name and view contents.

I've come up with this Regular expression string:


This will give me the view name in the first bracketed subselect.

Getting the view contents seems to be more difficult. I want to find all the text between these patterns:

FROM: the end of the last regular expression
TO a whitespace surround 'GO' string, so something like this:


The problem I have is a I cant 'not' match an entire string.

Can anybody help? I can reexplain if required!!!
Question by:nmcdermaid
    LVL 14

    Expert Comment

    by:Shiju Sasidharan
    what are the starting and ending pattern ?
    can u give a sample data and explain what all data u want to extract from that ?
    LVL 14

    Accepted Solution


    try this



    second bracketed subselect will give u the content

    it is better to use [ \t]  instead of [\s] if u need to match only tab and space

    [\s\S]*?     will get all contents including new lines until "GO"
    (this wont be greedy match and will stop searching on finding first "GO" )

    hope this willl help u


    LVL 30

    Author Comment

    Fantastic Shiju, I had a feeling that the non greedy match was what I wanted but I couldn't work out the syntax!!

    Thats great, its solved my problem.
    LVL 14

    Expert Comment

    by:Shiju Sasidharan
    hi nmcdermaid
    i am happy bcoz u r happy
    thank u for the points

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now