<?php $strings = [ "(a+b)", "(a+b)(ab+ba)", "(a+b)(ab+ba)(ab+ba)", "(ab+ba)", "a+b", " (a+b)(, (a+b)a" ]; $pattern = '/^\(a\+b\)(?:\(ab\+ba\))*$/'; foreach ($strings as $string) { preg_match($pattern, $string, $matches); print_r($matches); }
You have javascript disabled. You will not be able to edit any code.