Hello,

I was wondering if someone could tell me where I'm going wrong with my regular expression. I'm trying to write a regexp that identifies whether a string contains a correctly-formatted currency amount. I want to support dollars, UK pounds and Euros, but the example below deliberately omits Euros in case the Euro symbol get mangled anywhere in email or listserver processing. I also want people to be able to omit the currency symbol if they wish.

My regexp that I'm matching against is: "^\$\£?\d{0,10}(\.\d{2})?$"

Here's how I think it should work (but clearly I'm wrong, because it does not actually work):

^\$\£?      Require zero or one instance of $ or £ at the start of the string.
d{0,10}     Next, require between zero and ten alpha characters.
(\.\d{2})? Optionally, two characters can follow. They must be preceded by a decimal point.

Examples of acceptable input should be:

$12.42
$12
£12.42
$12,482.96  (now I think about it, I have not catered for this in my regexp)

And unacceptable input would be:

$12b.42
blah
$blah
etc


Here is my Python script:

#
import re

def is_currency(str):
   rex = "^\$\£?\d{0,10}(\.\d{2})?$"
   if re.match(rex, str):
      return 1
   else:
      return 0

def test_match(str):
   if is_currency (str):
      print str + " is a match"
   else:
      print str + " is not a match"

# All should match except the last two
test_match("$12.47")
test_match("12.47")
test_match("£12.47")
test_match("£12")
test_match("$12")
test_match("$12588.47")
test_match("$12,588.47")
test_match("£12588.47")
test_match("12588.47")
test_match("£12588")
test_match("$12588")
test_match("blah")
test_match("$12b.56")


AND HERE IS THE OUTPUT FROM THE ABOVE SCRIPT:
$12.47 is a match
12.47 is not a match
£12.47 is not a match
£12 is not a match
$12 is a match
$12588.47 is a match
$12,588.47 is not a match
£12588.47 is not a match
12588.47 is not a match
£12588 is not a match
$12588 is a match
blah is not a match
$12b.56 is not a match

Many thanks in advance. Regular expressions are not my strong suit :)

J-C

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to