I want to derive the place (string) and the amount (float) from a string using regex in PHP.
The target string would be formatted like this
7 Eleven Manly West 10.00
The place is a string of a-z, A-Z, 0-9 and hyphen characters.
The amount is in money format as so - 10.40 - 10 dollars and 40 cents. The amount can also be a negative, and doesn't retain a zero (4.30 - 4 dollars and 30 cents).
you don't have to use regex if you know it will always be formatted like this where the amount will be the last part. You can do
$data = explode(" ",$inputstring);
$amount = array_pop($data);
$place = implode(" ",$data);
but if you really want a regex try something like
(([a-zA-Z0-9]+\s)+)(-?[1-9][0-9]*\.[0-9]{2})
You can then get the place in the first back reference and the price in the 3rd
If you can guarantee that there will always be a price at the end this will match your string:
preg_match(/^(.*)(-?[0-9]+\.[0-9]+)$/, $yourString, $matches);
and $matches
will be
array(
0 => '7 Eleven Manly West 10.00',
1 => '7 Eleven Manly West ',
2 => '10.00'
)