0

I have to validate a syntax from math expressions like:

(ind(10)+15)-10
1000-(perg(25)*2)
25/var(1)
12*2-(58+1)/5

ind, perg and var are functions.

So, I'm trying this:

$var = '(\((var[\(])[0-9]+\)\)|(var[\(])[0-9]+\))';
$perg = '(\((perg[\(])[0-9]+\)\)|(perg[\(])[0-9]+\))';
$ind = '(\((ind[\(])[0-9]+\)\)|(ind[\(])[0-9]+\))';
$number = '[0-9]+(\.|,){0,1}[0-9]{0,2}';
$ope = '(\+|\-|\*|\/)';


$x = '('.$ind.'|'.$var.'|'.$perg.'|'.$number.')';
$a = $ope;
$s = '/^('.$x.$a.'(?R)|\('.$x.$a.'(?R)\)|(?R)'.$a.$x.'|\((?R)'.$a.$x.'\)|'.$x.')$/';

print_r(preg_match($s, '(perg(6)*perg(4)*)*1000000'));

And it's throwing this error:

Warning: preg_match(): Compilation failed: recursive call could loop indefinitely at offset 359

I gonna try to explain how I made this solution.

I though in this regular expression: 'X A S' or 'S A X'

X-> Values;    
A-> math operator;    
S-> a subexpression.    

A-> + or - or * or /;    
X-> ind(NUMBER) or perg(NUMBER) or ind(NUMBER) or NUMBER_FLOAT;    
S-> X A S or (X A S) or S A X or (S A X) or X    
3
  • See stackoverflow.com/questions/8440911/recursive-php-regex?rq=1 Commented Apr 20, 2017 at 20:31
  • I understand the problem are the anchors, but I couldn't understand how to salve this problem, Can you help me? Commented Apr 20, 2017 at 22:17
  • Someone can explain me more about this problem? I am note sure the problem are the anchors. Commented Apr 24, 2017 at 17:39

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.