Pr0no
Pr0no

Reputation: 4099

Regex to filter floating points

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

Answers (3)

Niet the Dark Absol
Niet the Dark Absol

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

danielpopa
danielpopa

Reputation: 820

This expression will solve your problem:

([\d.$])*

Upvotes: 0

Richard
Richard

Reputation: 4415

Have a look at this one: http://regexr.com?2vvpq

[0-9]+\.[0-9]{0,6}

Upvotes: 0

Related Questions