Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Help finding a valid statement in nested parens

Last post 11-17-2009, 1:05 PM by bp1222. 3 replies.
Sort Posts: Previous Next
  •  11-15-2009, 10:29 PM 57341

    Help finding a valid statement in nested parens

    Hi all,

     I'm trying to develop an expression that can determine if a statement is in a valid syntax.  The statement is contained within nested parens, and I need to see if the expressions not only is balanced (solved that), but valid.

     

    The following is my expression to see if the statement is balanced, but it lacks seeing if it is valid. 

    /^(\((?:[^()]+|(?1))*\))$/ 

     

    Statements can be of the following 2 examples:

    (1 = 1)

    or

    (1 => (3 = 3) || ((3 < (3 = 4)) && ((1 < 3) = 2)))

    or any breakdown there in where it follows the pattern of 

    ([number | nested boolean expression] [= | => | <= | < | > | && | ||]  [number | nested boolean BLOCKED EXPRESSION

     

    So far I've come up with the following, but they are not working right.

     15 define('RULE_OPERATOR', '=|==|>|>=|<|<=|!=');

     16 //define('RULE_VALID', '/^(\((?:[^(?:'.RULE_OPERATOR.')]*(?:'.RULE_OPERATOR.')[^(?:'.RULE_OPERATOR.')]*\))|(?1))*$/');

     17 //define('RULE_VALID', '/^(\((?:[^\((?:'.RULE_OPERATOR.')]*\s[(?:'.RULE_OPERATOR.')]\s[^)(?:'.RULE_OPERATOR.')]*|(?1))\))$/');

     

    I'm writing in PHP, so it's a PCRE based expression engine.

     

    Thanks for any advice or help.

    --

    Dave 

  •  11-16-2009, 12:42 PM 57349 in reply to 57341

    Re: Help finding a valid statement in nested parens

    You might consider just taking the easy way out and using PHP's eval function to test this:

    <pre>
    <?php
    function check($str){
            if(!preg_match('/^(\((?:[^()]+|(?1))*\))$/',$str)) return ("unbalanced.");
            if(!preg_match('/^[0-9|\s=<>()!&]+$/',$str)) return ("illegal characters found.");
            $return=eval("return true; if $str { }"); // doesn't execute the if $str code yet, just checks for syntax errors
            if(!$return) return ("syntax error.");
            return eval("return $str ? true : false;"); // no syntax error found, executes the code to evaluate result
    }
    $str='($test==1)'; // illegal characters found
    var_dump(check($str));
    $str='(1==1 || (2==)'; // unbalanced
    var_dump(check($str));
    $str='(1 => (3 = 3) || ((3 < (3 = 4)) && ((1 < 3) = 2)))'; // syntax errors, => should be >= and = should be ==
    var_dump(check($str));
    $str='(1 >= (3 == 3) || ((3 < (3 == 4)) && ((1 < 3) == 2)))'; // no syntax errors, evaluates true since 1 >= 1
    var_dump(check($str));
    $str='(1 >= 2)'; // no syntax errors and result is false
    var_dump(check($str));
    ?>

    Output:

    string(25) "illegal characters found."
    string(11) "unbalanced."
    string(13) "syntax error."
    bool(true)
    bool(false)
    

  •  11-16-2009, 12:43 PM 57350 in reply to 57341

    Re: Help finding a valid statement in nested parens

    dup post removed.


  •  11-17-2009, 1:05 PM 57370 in reply to 57349

    Re: Help finding a valid statement in nested parens

    Great, that is a step in the right direction!

     

    Sadly it doesn't account for the following 2 strings:

    $str1 = "(1==1==1)";

    $str2 = "(3 >= (4 == exit()))";

View as RSS news feed in XML