I have a string variable and want to extract year and number alone.
$val = '2015(15)';
preg_match ('/(.*?)\((.*?)\)/',$val,$match);
print_r($match);
Output: Array ( [0] => 2015(15) [1] => 2015 [2] => 15 )
Expected: the above is ok. or Array ( [0] => 2015 [1] => 15 )
$val = '2015';
preg_match ('/(.*?)\((.*?)\)/',$val,$match);
print_r($match);
Output: Array ( )
Expected: Array ( [0] => 2015 [1] => )
$val = '(15)';
preg_match ('/(.*?)\((.*?)\)/',$val,$match);
print_r($match);
Output: Array ( [0] => (15) [1] => [2] => 15 )
Expected: Array ( [0] => [1] => 15 )
Perhaps you can try something like,
/([0-9]{4})?(?:\(([0-9]*)\))?/