3

I'm working on a logic where user defined multiple conditions to validate and then they also defined in what manner that condition should evaluate, understand it like this, User defined 4 different conditions and when user defined the evaluation criteria, it defined like ((1 AND 2 AND 3) OR 4), So I wanted to validate that evaluation criteria with the help of regex, I build following function,

function validateCustomLogic(ele){
    var msg = '';
    var isError = false;
    var eleVal =  $(ele).val();
    if(typeof eleVal != 'unknown' && typeof eleVal != 'undefined' && eleVal != null && eleVal != ''){
        var regEx = /^([(]*[1-9]+[ ][AND|OR]\w*[ ][1-9]*[)]*)*$/gi;
        var matchEle = eleVal.match(regEx);
        if(matchEle == null){
           isError = true;
        }
    }
    else{
        isError = true;                
    }

    return isError;
}

this function called on my input change where user defined the evaluation criteria,

<input onchange="validateCustomLogic(this)"  type="text" />

my regex /^([(]*[1-9]+[ ][AND|OR]\w*[ ][1-9]*[)]*)*$/gi works for some pattern identification but not for all, below is the tested result,

Input                         Result            Expected Result
1 AND 2 AND 3 AND 4           Error: false      false 
1 AND 2 AND (3 AND 4)         Error: false      false 
1 AND 2 AND 3 (AND 4)         Error: true       true  
(1 AND 2 AND 3 AND 4          Error: false      true*
)1 AND 2 AND 3 AND 4          Error: true       true
(1 AND 2 AND 3) AND 4         Error: true       false*
(1 AND 2)(AND 3 AND 4)        Error: true       true
((1 AND 2 AND 3 AND 4)        Error: false      true*

If you see the test result some of the entries with * are not satisfied with this regex, not sure what I'm missing over here. Can anyone help me to validate that.

user2809299
  • 149
  • 2
  • 13
  • You might wanna keep track of number of opening and closing brackets. – Shub May 24 '18 at 07:25
  • I know regex might not be do that work on number of opening and closing brackets, but still it should satisfy the `(1 AND 2 AND 3) AND 4` condition, but am open to have other suggestions as well, a logic who do everything – user2809299 May 24 '18 at 07:28
  • check this [answer](https://stackoverflow.com/questions/133601/can-regular-expressions-be-used-to-match-nested-patterns) – Shub May 24 '18 at 07:48
  • 1
    `^(\()?(?:[1-9]+[ ][AND|OR]\w*[ ][1-9]*)*(?(1)\))$` using conditional regex to validate 1st level of brackets. – Shub May 24 '18 at 07:49
  • I tried, but it seems to be some issue in the regex, have you validate it on https://regexr.com/ – user2809299 May 24 '18 at 09:19
  • I did it on [regex101](https://regex101.com/r/eBHLfW/1) – Shub May 24 '18 at 10:20
  • How deep can this nesting go? – Tamas Rev May 24 '18 at 13:40
  • As such there is no restriction I've build in the code, its up to user how many conditions are there and how complex the evaluation criteria for those conditions – user2809299 May 25 '18 at 06:50

1 Answers1

0

https://regex101.com/r/anq7Mc/2

/^\((\d+\)? (AND|OR) )+\d+(\)|\) .*?)$|^(\(?\d+\)? (AND|OR) )+\d+(\)|\) .*?)$|^(\d+ (AND|OR) )+\d+$/

This match all you need

enter image description here

Piterden
  • 771
  • 5
  • 17