Edmon
Edmon

Reputation: 4872

Parsing select statement with regex (for custom SQL parser)

I am doing this for didactic purposes (building a teaching database; building parts of it just as full-blown databases have it; this is for a very simple query parser.)

I am trying to parse out simple "select" statement using regex. It works for most simple cases, but I am losing it at the balance of allowing spaces between selection of tables (from a,b;), allowing unlimited spaces between from and terminator ;, and allowing for optional restrictor "where" and unlimited spaces between from a,b where and ;

Here is the regex:

select\s(.*)\sfrom(.*)\s(where (.*))?\s;

and here are sample queries:

select a.a,b.b,c from a,b where a.a = b.a;

select a.a,b.b,c from a,b;

select a.a,b.b,c from a,b ;

select a.a,b.b,c from a,b where a.a = b.a  ;

Available on regex 101 with unit tests.

Upvotes: 4

Views: 9087

Answers (2)

Foxtrot
Foxtrot

Reputation: 637

This will work for the examples provided : select\s+(.*?)\s*from\s+(.*?)\s*(where\s(.*?)\s*)?;

See here : https://regex101.com/r/sBwpok/3

Upvotes: 2

Aimee Borda
Aimee Borda

Reputation: 842

The regex select\s+(.*)\s+from\s+(.*)\s*(where .*)?; passed all the tests. The only modification I added is \s+ rather than a single space \s

Upvotes: 2

Related Questions