0

I have a Regex on SQL problem here. for SQL union query of the form:

Select a From (subquery1) union (subquery2)

I though this would be a simple Regex to match things within the From clause, I had the following Regex:

\((?<subquery1>.*)\) union \((?<subquery2>.*)\)

But later I discover things could go recursive, the 2 subquery could also contain union case again, For example:

(
(select a from b) union (select x from b)
) union (
(select a from b) union (select x from b)
)

And this completely mess up my Regex capture, subquery1 would capture the first union instead of the second one if it is non greedy, and it would capture everything until third union if it is greedy. I had also tried adding the non-greedy ? operator to both capture clause but no luck there.

I need enlightenment please, many thanks

PS: I had a custom database sit on top of MS SQL server 2012, I need to parse standard SQL select query, and made some modification to table names; numerical operation to few columns. Thus generating a modified but still standard SQL query, and pass it to SQL server

Ben.Yan
  • 118
  • 4
  • 13
  • What DBMS are you using? – mbeckish Jun 20 '13 at 12:48
  • Considering your example, what is the desired result? – Ahmed KRAIEM Jun 20 '13 at 13:01
  • @mbeckish,@Ahmed KRAIEM. Hi guys, My program connect to Microsoft SQL Server 2012. the desired outcome, is Csharp Regex can capture the subquery string, which my other Regex logic will go through them to find appropriate ground terms. – Ben.Yan Jun 20 '13 at 13:39
  • I see this as not-a-dup because it needs a specialized form of the RegEx. – Kelly S. French Jun 20 '13 at 16:55
  • I had check the RegEx for parsing SQL query in C# post, it does not take into account the sub-query, I am now thinking of adding non-SQL symbol just to help parsing. I am dealing with academic problem and SQL is just a syntax sugar to represent underlying Relational Calculus, i can afford making a new variant of SQL – Ben.Yan Jun 20 '13 at 17:14

0 Answers0