phpregexfloating-pointtext-extraction

Get float number in string which is nor preceded by $ and not followed by %


I would like to find specific float number from string using regex .
string

202-715-1278 2 0.01% 0.30 0.00% $0.00 0.00%

The only number I need to find this one 0.30.

I tried a lot of patterns but all of them return the whole float numbers within the string and some of them not working at it all

[-+]?([0-9]*\,)?[0-9]+
\d+(?:\.\d+)?

also I tried

floatval()

but it doesn't work neither.


Solution

  • Try this:

    (?<!\$)\b[-+]?\d+\.\d+\b(?!%)
    

    It matches a number with a decimal point in it, but not preceded by $ or followed by %.

    See RegExr