Search code examples
regexregex-group

Regex to get the right most number after $


I have the following string and I am trying to get the right most numbers from them.

Input:

-30%$13.99$19.99
$19.99

Output:

$19.99
$19.99

What is the best way for me to do this with regex?

Here is what I have so far:

[\$ ]+?(\d+([,\.\d]+)?)

Solution

  • /\$[0-9]+(\.[0-9]+)?$/mg
    
    /\$\d+(\.\d+)?$/mg
    

    Tested with ECMAScript (JavaScript) and PCRE2 (PHP >=7.3) on the string that you provided

    • [0-9]+(\.[0-9]+)? stands for float or integer numbers.
    • The last $ stands for the end of the line.