1

I've been stuck on some complex Regex writing. I have some textual request - like a SQL request - that I've to transform into a JSON object in Javascript. To do it, I was thinking about making a regex with a pattern which could be repeated n times.

Here is a request example:

INSERT INTO {{ruleName}} RULES {{conditions}} WHEN {{true|false}}

My main problem in here is the conditions part as it can contain a basic condition (object.property {=|!=|<|<=|>|>=} value) or multiple and nested conditions with {AND|OR} or parenthesis (same rule as mathematics).

To catch the first condition, I wrote this regex:/(\s*(\w+.?\w*)\s*(=|!=|>|<|<=|>=)\s*'(.*?)'\s*)/gi

But I cannot manage to write a good regex for the multiple condition or nested condition case.

This is my regex at the moment: /(\s*(\w+.?\w*)\s*(=|!=|>|<|<=|>=)\s*'(.*?)'\s*)((\bAND\b|\bOR\b)(\s*(\w+.?\w*)\s*(=|!=|>|<|<=|>=)\s*'(.*?)'\s*)\s*){0,}/gi

Can anyone help me about it ?

EDIT: As asked, here are two examples of multiple or nested conditions

  • {condition1} AND {condition2} OR {condition3} multiple conditions here
  • {condition4} AND ({condition5} OR {condition6}) nested conditions there
TimmyMdfck
  • 33
  • 7
  • How about using boundaries? e.g. `.*(?= WHEN)` which will match until it is followed by `WHEN` ? Note that won't be very computationnally efficient, but it's very efficient in regard to your coding time ! ;) – Aaron Jul 20 '18 at 12:48
  • I already got this, I have a function that returns all the strings between my main request markers (INSERT, RULES, WHEN). The problem is that one of those strings could be a repeated pattern, and there comes my question :) – TimmyMdfck Jul 20 '18 at 12:57
  • Oh yeah nevermind. I'll dive into your regex then – Aaron Jul 20 '18 at 13:05
  • Can you add an example of nested condition? – Aaron Jul 20 '18 at 13:08
  • edited the main post, just ask me if you need more infos – TimmyMdfck Jul 20 '18 at 13:12
  • You may not be able to do it with javascript. At least not easily. Please see: https://stackoverflow.com/a/133684/7500028 (and first comment). Also see: https://stackoverflow.com/a/4414453/7500028 – Julio Jul 20 '18 at 13:27
  • I'm not sure what you're trying to do actually : if you need each condition separately and don't need to bother with the AND/ORs and parenthesis then your first regex is (nearly?) fine, if you need to preserve the AND/ORs and parenthesis then you only need to extract the whole `{{conditions}}` string. Am I missing something? – Aaron Jul 20 '18 at 13:34
  • https://regex101.com/r/aljzPr/1 here you have an example with 3 conditions. The actual regex only matches the first and the last, avoiding the other between – TimmyMdfck Jul 20 '18 at 13:39
  • 1
    How about `[.\w]+\s*(=|!=|>|<|<=|>=)\s*'?[^']+'?` or `[.\w]+\s*[=!<>]{1,2}\s*'?[^']+'?` ([regex101](https://regex101.com/r/6NNmSP/2)) ? Might need to be refined for edge cases, but there no real nesting problem here (you don't have a format you want to match that can contain itself) – Aaron Jul 20 '18 at 13:43
  • 1
    Better yet, `\S+\s*[=!<>]{1,2}\s*(?:'[^']*'|\S+)` ([regex101](https://regex101.com/r/6NNmSP/3)) – Aaron Jul 20 '18 at 13:49
  • Yeah, that seems about right for the multiple case. I'm going to abandon the nested case for now as it is really complex. Thanks for helping out ! – TimmyMdfck Jul 20 '18 at 13:50
  • Fixed for nested cases (just had to make sure not to match parenthesis) : `[^\s()]+\s*[=!<>]{1,2}\s*(?:'[^']*'|^\s()]+\b)` https://regex101.com/r/6NNmSP/5 – Aaron Jul 20 '18 at 13:54
  • The linked duplicate has absolutely nothing to do with the problem, there's no nested pattern here. Why was this closed with only one vote? :-/ – Aaron Jul 20 '18 at 13:57

0 Answers0