I need to extract just 94.00
from the string below.
Which regex would I use to do so? My current regex of [^$]+$
is returning $99.00 (176)
5 Easy Payments $94.00 $99.00 (176)
I need to extract just 94.00
from the string below.
Which regex would I use to do so? My current regex of [^$]+$
is returning $99.00 (176)
5 Easy Payments $94.00 $99.00 (176)
It seems like you might want to use this regular expression:
\$[\d.]+
This matches all prices, so depending on your language, juse use the function to get the first occurrence. However, this will also match something ridiculous like $......
as a price.
\$[\d]+\.[\d]+
will only match prices in the form $123.456
with an arbitrary number of digits.
^[^$]+\$[\d]+\.[\d]+
will match everything up to the last digit in the price. This is useless in its current state because it returns 5 Easy Payments $94.00
for your test string.
^[^$]+(\$[\d]+\.[\d]+)
will match the same thing as above, but Group #1 of the regular expression will be the price $94.00
. If you want to only match the number itself, move the left bracket over so it becomes ^[^$]+\$([\d]+\.[\d]+)
.
The issue is that this will return no matches if there's a non-price dollar sign somewhere before the first valid price. As long as that's not an issue, then ^[^\$]+\$([\d]+\.[\d]+)
will give you 94.00
for Capture Group 1.
You need to use a capturing mechanism if you cannot use any normal programming method to control the amount of matches you get:
/(\$\d+(?:\.\d+)?)[\s\S]*/
This regex finds the first (because any regex engine processes the string from left to right, at least by default) $
+1 or more digits
+an optional sequence of '.' and 1 or more digits
, captures this text into Group 1 (with (\$\d+(?:\.\d+)?)
) and then matches and consumes the whole rest of the string with [\s\S]*
(any 0 or more chars).