It's because of the greediness of "?
", try "+
" as in :
$pattern = '/(?:\d+(?:\.\d+)?\D)+\d+(?:\.\d+)/';
$input = '34.27-15.44/8.44';
echo $input."\n";
preg_match($pattern, $input, $match);
print_r($match);
As for your edit:
$pattern = '/(?:\d+(?:\.?\d+)?\D)+\d+/';
$input = [
'1.0+2.5*5.4',
'5*8-4',
'5*8-4string',
'string5*8-4',
'string'
];
foreach($input as $string){
preg_match($pattern, $string, $match);
print_r($match);
}
It gives:
Array
(
[0] => 1.0+2.5*5.4
)
Array
(
[0] => 5*8-4
)
Array
(
[0] => 5*8-4
)
Array
(
[0] => 5*8-4
)
Array
(
)
as I suspect you want it to.
http://codepad.viper-7.com/4FoQnB