469,271 Members | 1,719 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

using regex to remove $ sign

Kun
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.
Apr 11 '06 #1
4 10189
"Kun" <ne*******@gmail.com> wrote:
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.


RE? ex-perler? try strip+lstrip instead:
text = " $12921 "
text.strip().lstrip("$")

"12921"

</F>

Apr 11 '06 #2
Fredrik Lundh>RE? ex-perler? try strip+lstrip instead:<

Or even:
text = " $12921 "
text.replace("$", "")

' 12921 '

Bye,
bearophile

Apr 11 '06 #3

be************@lycos.com wrote:
Fredrik Lundh>RE? ex-perler? try strip+lstrip instead:<

Or even:
text = " $12921 "
text.replace("$", "")

' 12921 '


That's fair enough with the given input, but it would silently swallow
the "$" in "123$5678" -- this sort of approach leads to all sorts of
disasters. The next problem after you've got rid of the $ is that
you'll find commas in it. No worries, we'll just rip out any commas
too!! So what happens is this: punter is thinking twelve dollars and 34
cents but types in 12,34 either because he's European or (more likely)
because he made a typo (comma is next to period on most folks
keyboards) then some twit programmer blindly rips out the comma and hey
presto you've got twelve hundred and 34 dollars. Think it's unlikely?
Just check out the bidding bidding on a certain large well-known online
auction system :-)

Apr 11 '06 #4
> 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

Apr 11 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by David | last post: by
7 posts views Thread by bill tie | last post: by
4 posts views Thread by Brian Henry | last post: by
7 posts views Thread by Extremest | last post: by
1 post views Thread by AMP | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.