2

I have a string, where price value($544.50) can be anywhere in the string. eg: HP G60-630US Notebook: Laptops | RadioShack.com --> $259.97 (radioshack.com)

I need to get the value: 259.97 out of the string

For this i tried using some random regex's. But no luck!

Can someone help me, giving a proper regex string for retrieving this price value from a string.

Thanks in advance.

Shirish
  • 77
  • 1
  • 1
  • 5

7 Answers7

13

This regex will work with multiple currencies: $ £ €

[\$\£\€](\d+(?:\.\d{1,2})?)

Example

SERPRO
  • 10,015
  • 8
  • 46
  • 63
  • [\$\£\€](\d+(?:\.\d{1,2})?) Its working! One more lame question. In Yahoo pipes, using regex module, after matching we need to give a value in the value field. So that i can only get the number out of the complete string. Can u help ? – Shirish Jan 16 '12 at 12:25
  • The number is in the first match of the regex. Usually match[0] is the complete string and match[1] is the number you are looking for. I don't know about yahoo pipes. but can you choose the match you want to use? – SERPRO Jan 16 '12 at 12:45
1

[$]?[0-9]*(\.)?[0-9]?[0-9]?

This should match those with a $ sign and those without.

Neil Knight
  • 47,437
  • 25
  • 129
  • 188
0

This one finds first decimal number in a string:

^[^\d]*(\d+|\d+((,|\.)\d{1,2}))(\s|[a-zA-Z)]|€|$).*
  • looks only for numbers with 0-2 decimals decimal point or comma allowed
  • no need for spaces before or after the number
  • number must not end with any delimiter (eg. 12.12. or 12! is not returned)
  • number can end with Euro sign €
  • works on US negative numbers ($1233.50)
besimple
  • 444
  • 1
  • 7
  • 12
0

Here's a way to replace and extract

Extract:

preg_match('/\$([0-9]+[\.]*[0-9]*)/',$string,$matches);
foreach($matches as $match) {
  echo $match."<br>";
}
//echo $matches[0]; for just the first match

Modify/Replace:

$string = preg_replace_callback('/\$([0-9]+[\.]*[0-9]*)/', function ($matches) {


            foreach ($matches as $match) {
                // only if value is !empty and higher than 0
                $tmp = str_replace("$", "", $match);
                if ($tmp > 0 && $tmp != "") {
                    //do something to $tmp in this case round to two decimal places
                    return "$" . round($tmp,2);

                }

            }

        }, $string);

There's probably a better way to handle the symbol on my replacement example.

Joel Davey
  • 2,363
  • 1
  • 21
  • 20
0

Regex to filter out number: String regex = ([0-9]+[, .]?)+ You can use it like this:

CurrencySymbol + regex regex + CurrencySymbol

Mudit
  • 41
  • 1
  • 5
0

Something like this will probably work:

/\$(\d*\.?\d+?)/
Paul Bain
  • 4,364
  • 1
  • 16
  • 30
0

\$(\d*\.?\d*) should work, not optimal but probably useful.

(Important: escape $ and ., which are special characters)

Rumpel
  • 27
  • 4
  • This will match something like this too: `$300 rfersijfurhfurnfuerfnurjfrifnrunfurfrfurhufrhfhr 4594545` – noob Jan 16 '12 at 12:14
  • @micha: I forgot to escape the ., yes. But even without that I don't see how your string would match? d matches a digit, . anything (but once maximum). I used * instead of + to allow for prices such as $.50 – Rumpel Jan 16 '12 at 12:44