fork(2) download
  1. <?php
  2.  
  3. $re = '#^(([^()]|\((?1)\))*)$#';
  4.  
  5. foreach (array('', '1', '()', '(())', '1(2)3', '(()())', '()()', '()x()1') as $value)
  6. echo "'$value' (OK) -> " . (preg_match($re, $value) ? 'OK' : 'invalid') . "\n";
  7.  
  8. foreach (array('1(', ')', '(()', '12)3', '((())', '()(()') as $value)
  9. echo "'$value' (invalid) -> " . (preg_match($re, $value) ? 'OK' : 'invalid') . "\n";
  10.  
Success #stdin #stdout 0.02s 52432KB
stdin
Standard input is empty
stdout
'' (OK) -> OK
'1' (OK) -> OK
'()' (OK) -> OK
'(())' (OK) -> OK
'1(2)3' (OK) -> OK
'(()())' (OK) -> OK
'()()' (OK) -> OK
'()x()1' (OK) -> OK
'1(' (invalid) -> invalid
')' (invalid) -> invalid
'(()' (invalid) -> invalid
'12)3' (invalid) -> invalid
'((())' (invalid) -> invalid
'()(()' (invalid) -> invalid