I need to parse simple query construction possible options
select col1,col2
select col1,col2 where col1=1 and col2 = 'title'
select col1,col2 where col1=1 and col2 = 'title' order by col1
select col1,col2 order by col1
I have following regex
(select-?.*?)\s+(.*?){0,1}(?:\s+(where-?.*?)(.*)){0,1}\s(order by-?.*?){0,1}\s{0,1}
But it works strange in my case

I expected order by to be in Group 5, but in my case, it is a part of Group 4. I would like to have following order
select wordwhere word - Is optionalorder by word - Is optionalSo, could someone point me, what I'm doing wrong ?
The problem is here with group 4: (.*)
Greedy .* eats up the entire line. The regex has to backtrack to match \s, and the space before col1 is the first space from the right, so it is matched. All the other groups are optional, so the regex is done.
Edit: OK, you want a regex, too...
This regex works on all the input given (may need adjustments):
^(select-?[^\n]*?)(\s+[^\n]*?)?(?:\s+(where-?[^\s]*\s)([^\n]*?))?\s(?:(order\s*by-?\s)([^\n]*))?$
Use it with mg modifiers.
It's loosely based off of your regex, but I can explain it if need be.
For convenience, here is the same regex with named groups (mgx modifiers needed): ^(?'select'select-?[^\n]*?)
(?'col'\s+[^\n]*?)?
(?:\s+(?'where'where-?[^\s]*\s)
(?'cond'[^\n]*?))?\s
(?:(?'order'order\s*by-?\s)(?'by'[^\n]*))?$
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With