[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1201
  • Last Modified:

Parsing SQL Script with Regular Expression

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
...
..
..
..

GO


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

I've come up with this Regular expression string:

CREATE[\s]*VIEW[\s]*([\S]*)[\s]*AS


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:

([^[\s]GO[\s]])[\s]GO[\s]


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

Can anybody help? I can reexplain if required!!!
0
nmcdermaid
Asked:
nmcdermaid
  • 3
1 Solution
 
Shiju SasidharanCommented:
what are the starting and ending pattern ?
can u give a sample data and explain what all data u want to extract from that ?
;-)
Shiju
0
 
Shiju SasidharanCommented:

try this
'-----------------------------------------------------------------

CREATE[\s]*VIEW[\s]*([\S]*)[\s]*AS([\s\S]*?)GO

'-----------------------------------------------------------------

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

;-)
Shiju




0
 
nmcdermaidAuthor Commented:
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.
0
 
Shiju SasidharanCommented:
hi nmcdermaid
i am happy bcoz u r happy
thank u for the points
;-)
Shiju
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now