Parsing select statement with regex (for custom SQ

2019-07-29 02:27发布

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.

2条回答
孤傲高冷的网名
2楼-- · 2019-07-29 03:22

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

查看更多
放荡不羁爱自由
3楼-- · 2019-07-29 03:23

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

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

查看更多
登录 后发表回答