473,395 Members | 2,437 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 11592
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: David | last post by:
Hi, I've had a search through google but couldn't really find the answer I was looking for.I'm new to PHP, so please take it <relatively> easy. I've created a script which runs some SNMP...
5
by: Henry | last post by:
I have this simple code, string escaped = Regex.Escape( @"`~!@#$%^&*()_=+{}\|;:',<.>/?" + "\"" ); string input = @"a&+" + "\"" + @"@(-d)\e"; Regex re = new Regex( string.Format(@"(+)", escaped),...
7
by: bill tie | last post by:
I'd appreciate it if you could advise. 1. How do I replace "\" (backslash) with anything? 2. Suppose I want to replace (a) every occurrence of characters "a", "b", "c", "d" with "x", (b)...
2
by: JebBushell | last post by:
I see signs that the ASP.NET regular expression validator has a different instruction set that the Find utility in VS 2003. I am trying to use the VS2003 regular expression Find tool to test...
4
by: Brian Henry | last post by:
I have phone numbers like this in a data table 123-435-1234 1231231234 432.234.2321 they all have different formatting, what I want to do is get them all formatted like this (123) 123-1234
7
by: Extremest | last post by:
I am using this regex. static Regex paranthesis = new Regex("(\\d*/\\d*)", RegexOptions.IgnoreCase); it should find everything between parenthesis that have some numbers onyl then a forward...
24
by: garyusenet | last post by:
I'm working on a data file and can't find any common delimmiters in the file to indicate the end of one row of data and the start of the next. Rows are not on individual lines but run accross...
1
by: AMP | last post by:
Hello, I am coming back to a project and I dont remember what the following Regex says I do know it removes all \r\n from the string, but I dont see how. Can someone explain this one? Regex re...
14
by: Andy B | last post by:
I need to create a regular expression that will match a 5 digit number, a space and then anything up to but not including the next closing html tag. Here is an example: <startTag>55555 any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.