Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegExp to parse currency value

I would need to write a RegExp in AS3 which parses an Excel formatted currency value into a number: E.g. RegExp($35,600.00) = 35600

And checks if it is correctly formatted (with the "," as the thousands separator, and the "." as the decimal point. The currency symbol could be any (not just $) and could stand at the beginning or the end.

So I would just need to strip every non digit from the number and check if is valid.

Thanks! Martin

like image 905
Martin Avatar asked Feb 06 '26 00:02

Martin


1 Answers

You will need two cases, one for comma as separator and another for decimal separated whole numbers.

If a whole number, remove everything after the comma or decimal point(depending on your format). Then run the following Regex:

This will remove all non-digit characters:

s/\D+//g;

If you do not have a whole number, you will need to include an exception for the whole number separator:

Decimal separator:

 s/[^\d.]+//g

Comma separator:

 s/[^\d,]+//g

*Disclaimer: I only parsed these regexes in my head, so my syntax could be slightly off.

like image 164
Chris Ballance Avatar answered Feb 08 '26 03:02

Chris Ballance