Reputation: 4099
Consider the following string:
I bought 70 apples for $15.45, 2 cars for 23.000 each
I am in search of a regex that can return the following result:
I bought {NUMBER:70} apples for ${NUMBER:15.45}, {NUMBER:2} cars for
{NUMBER:23.000} each
I believe the range I would like to filter is a floiting point with (max) 6 digits. I don't know if this would have to be specified.
Any help would be greatly appreciated :)
Upvotes: 0
Views: 280
Reputation: 324650
Try this:
$text = preg_replace("((?<!{NUMBER:)([0-9]+(?:\.[0-9]+)?)(?!}))","{NUMBER:$1}",$text);
This will do what you asked, but also includes a check to ensure that the number hasn't already been put in a {NUMBER:#}
container ;)
Without the check:
$text = preg_replace("([0-9]+(?:\.[0-9]+)?)","{NUMBER:$0}",$text);
Upvotes: 1
Reputation: 4415
Have a look at this one: http://regexr.com?2vvpq
[0-9]+\.[0-9]{0,6}
Upvotes: 0