4

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 Answers 2

2

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

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.