Reputation:
Consider the following queries:
select
foo,
bar,
yourmom
from
theTable
select top 10 *
from
theTable
select distinct foo, bar + 1, yourmom from theTable
I want a regex query that would extract:
foo,
bar,
yourmom
*
foo, bar + 1, yourmom
respectively.
I tried ^\sselect\s(distinct\s)?(top\s\d*)?(?'columns'.*\s)from[\s.]*$
, which I thought would work, but doesn't. I've been playing with it for a while now and I still cannot get it to work in all three test cases. Can someone help me with their regex-fu?
Upvotes: 3
Views: 2550
Reputation: 4446
Edit: First you need to make .-match every character including newline. In java you can set the DOTALL flag, but in C# I believe you should use the RegexOptions.SingleLine
option.
Then this expression should work:
^\s*select\s+(?:distinct\s+)?(?:top\s+\d*\s+)?(?'columns'.*?)from.*$
Upvotes: 3
Reputation: 57202
I think that it would actually be easier to write a "proper" parser for SQL queries (check Irony: it's awesome and comes with a SQL example) than using regular expressions.
Upvotes: 1