We help IT Professionals succeed at work.

Help with Regex

interclubs
interclubs asked
on
I have the following regular expression, which I am trying to use to extract dollar amounts. I want it to only pull out values that either have a decimal point followed by 2 digits, or a dollar sign.

So in the below example, these would not be a match: #2343434 222 123-2333 $12.0
<?php
$text = '#2343434 222 123-2333 $12.0 $34.00 34.00 1,000.00 $3';
if(preg_match_all('/\$?\d+(?:\.\d+)?/ims',$text,$matches)){
	trigger_error(var_dump($matches, true));
}
?>

Open in new window




Comment
Watch Question

To extract just dollars (with or without the dollar sign) you'd use something like this:
 
<?php
$text = '#2343434 222 123-2333 $12.0 $34.00 34.00 1,000.00 $3';
if(preg_match_all('/\$?\d*\.\d\d/',$text,$matches)){
	trigger_error(var_dump($matches, true));
}
?>

Open in new window

Author

Commented:
thanks!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.