I am making an API and I want to implement an option to filter so I want to validate the expression with a regexp. this is the expression send in request.
filter[price] = gt.5 and lt.10
The rules: the first characters represent the operator to be used example:
gt = '>'
ge = '>='
lt = '<'
le = '<='
eq = '='
ne = '<>'
like = 'like'
ilike = 'ilike'
then there is a point to separate the value to be compared and optionally there would be other expression separated by the 'and' or 'or' operators.
This is the regex that I have built
$re = '/([a-z]{2,5}\.[\w0-9]+)(\s)*(?(2)(and|or)\2+([a-z]{2,5}\.[\w0-9]+))/i';
I want to make the regex take as valid the following cases and that I take as invalid any other case different from those
case 1: gt.5 (valid)
case 2: gt.5 and lt.10 (valid)
case 3: gt.5 and lt.10 or eq.5 (valid)
when I test the regex it works for both cases but it also matches for the following cases but these should be considered invalid.
case 4: gt.5lt.10 (should be invalid)
case 5: gt.5and (should be invalid)
case 6: gt.5..lt.10 (should be invalid)