Need some regular expressions for numbers with dollar signs and commas or periods?
I came across a scenario where we needed to filter field values which could possibly include text and all other string characters and just perform action on some dirty numbers. By dirty numbers I mean data for money and square footage, which would include dollar signs ($), periods (.) and commas (,).
The data needed to be filtered to determine if did just contain numbers, AND commas, dollar signs or periods. Any fields with text or other characters were to be ignored.
Here is the regular expression, reg ex, used:
^[,0-9\.\$]+$
The CFML REFind check looks like this:
REFind("^[,0-9\.\$]+$", fieldValue)
with "fieldValue" being the variable.
We just wanted to remove the commas from all numeric values and change commas in text values to semi colons. The whole regex check and action looks like this:
Just a few minor notes... The dollar sign ($) and period (.) are special characters and have to be escaped by preceding with a forward slash (\).
Here are some example tests used:
12344,234,345 = #ReFind("^[,0-9\.\$]+$","12344,234,345")#
$12344,234,345 = #ReFind("[,0-9\.\$]+$","$12344,234,345")#
$12344,234.345 = #ReFind("[,0-9\.\$]+$","$12344,234.345")#
12344.234.345 = #ReFind("^[,0-9\.\$]+$","12344.234.345")#
as$dfa2,32344 = #ReFind("^[,0-9\.\$]+$","as$dfa2,32344")#
asdfasdf,rt$yu = #ReFind("^[,0-9\.\$]+$","asdfasdf,rt$yu")#
$123412.4567 = #ReFind("^[,0-9\.\$]+$","$123412.4567")#
asdfa,12341.3456.sdfg,dgfh = #ReFind("^[,0-9\.\$]+$","asdfa,12341.3456.sdfg,dgfh")#
It cost me $33.00 = #ReFind("^[,0-9\.\$]+$","It cost me $33.00")#
It cost me $33 = #ReFind("^[,0-9\.\$]+$","It cost me $33")#
It cost me 33.00 = #ReFind("^[,0-9\.\$]+$","It cost me 33.00")#