Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse simple query construction using regex

Tags:

regex

sql

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

enter image description here

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

  • Group #1 - select word
  • Group #2 - columns string
  • Group #3 - where word - Is optional
  • Group #4 - conditions string - Can be optional (if no where present)
  • Group #5 - order by word - Is optional
  • Group #6 - list of order by's - Is optional (if no order by present)

So, could someone point me, what I'm doing wrong ?

like image 429
Ivan Ursul Avatar asked Feb 01 '26 11:02

Ivan Ursul


1 Answers

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]*))?$

like image 111
Laurel Avatar answered Feb 03 '26 03:02

Laurel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!