using regex to remove $ sign

Tim Chase python.list at tim.thechases.com
Tue Apr 11 08:00:22 EDT 2006


> i have an html/cgi input that takes in values to a mysql
> database, however, if i stick in $20 instead of 20, it
> crashes the program because of the extra $ sign.  I was
> wondering if anyone has a quick regular expression in
> python to remove the $-sign if it is present in the
> input.

While the others have provided non-regexp solutions, I 
suspect there's a deeper underlying problem here if a simple 
dollar-sign is causing the program to die.

If you find where this death happens, there's usually an 
associated escape() function that will handle all the 
troublesome characters.  My suspicion is that you're trying 
to stick this string value into a numeric field in your 
database.  Thus, you want to strip out *anything* that will 
cause a mysql assignment-to-a-numeric-field to barf.

If you really must do it with a regexp:

Just strip a dollar-sign:

	result = re.sub(r'\$','',input_value)

If you want to strip any non-numerics:

	result = re.sub(r'[^0-9]', '', input_value)

If you want decimal points too:

	result = re.sub(r'[^0-9.]', '', input_value)

As someone else mentioned, you might want to take other 
currency conventions (namely, using commas rather than 
periods) into consideration.  Thus, I'd do it in a two-step

	result = re.sub(r'[^0-9.]', '',
		input_value.replace(",", "."))

This normalizes all commas to periods and then strips out 
anything that isn't a digit or a period.  This will still 
give your program grief if someone puts in something like 
"$192.168.3.14".

Thus, you might want to just pull out the dollars and 
optional cents, and use them:

	r = re.compile(r'.*?(\d+)([.,]\d\d)?.*')
	m = r.match(input_value)
	if m:
		dollars = m.group(1)
		cents = m.group(2)
		if not cents:
			cents = cents[1:]
		else:
			cents = "00"
	else:
		raise BogusValueFromDoofusError

	new_input_value = "%s.%s" % (dollars, cents)
	
With the above bogus IP-address/currency value, this would 
produce a valid result of "192.16" which may or may not be 
what you want.  Caveat regextor.

Feel free to monkey with the regexp to adjust for your wants.

-tim








More information about the Python-list mailing list