473,586 Members | 2,678 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11627
"Kun" <ne*******@gmai l.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().ls trip("$")

"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.rep lace(",", "."))

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_v alue)
if m:
dollars = m.group(1)
cents = m.group(2)
if not cents:
cents = cents[1:]
else:
cents = "00"
else:
raise BogusValueFromD oofusError

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
4680
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 queries. I'd like help if possible in formatting one of the values it returns.
5
2314
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), RegexOptions.CultureInvariant ); string s = re.Replace( input, "" ); It doesn't seem to work, regular expression return without filter out any...
7
2605
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) every occurrence of characters "p", "q", "r", "s" with "y". Right now, I do it as follows:
2
1489
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 regular expressions for use in a ASP.NET validator. The results I am getting are inconsistent . What works in VS does not always work in ASP.NET and...
4
4539
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
2575
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 slash then some numbers. For some reason I am not getting that. It won't work at all in 2.0
24
4819
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 multiple lines. It would appear though that every distinct set of data starts with a 'code' that is always the 25 characters long. The text is variable...
1
278
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 = new Regex(@"(+)", RegexOptions.Compiled); string op = re.Replace(FileToParse, "");
14
4972
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 text</aClosingTag> I need a Regex that will get all of the text between the html tags above (the html tags are random and i do not know them before...
0
7912
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8202
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8338
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7959
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8216
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3837
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.