Regular Expression Help
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
Puzzled about this regex
Hello, I wonder if someone could tell me where I am going wrong with my regular expression, please. My regex only matches the text I'm looking for (a number followed by a distance unit) when it appears at the beginning of the string. But I am not using the ^ character (which would indicate that I only want a match if it is at the start). --- # import re regex1 = re.compile("[0-9]+ (feet|meters)", re.IGNORECASE) def is_distance(str): if regex1.match(str): print "distance" else: print "not distance" # First one matches, second does not -- WHY? is_distance("300 feet is the distance") is_distance("The distance is 300 feet") --- Thank you! J-C -- http://mail.python.org/mailman/listinfo/python-list
Re: Using poplib to parse headers - Thank You All!
Tim Roberts wrote: You've received some very confusing advice in this thread. Alex had the right answer, but I want to expand it a bit. [...] poplib.retr gives you a string. You need to hand that string to the email module, and you do that using "email.message_from_string". This is just to thank Tim, Alex, and everyone who posted for their help in getting this to work. It's been more than a month since you answered my question and I've been having to focus on other things. Last week, I finally got back to this task and your answers helped me get it working. To save time for future people who might have the same question, I'm posting a basic script for reading mail from a POP server and passing the email message to the Python email library for parsing. I'm new to Python (but experienced in other languages) so please feel free to tell me if any of my syntax is clumsy, or if I did something the difficult way when Python has an easier or more efficient way to do it. I hope this will be useful for the archives. # import getpass, poplib, email # Set up the connection to the POP server popconnection = poplib.POP3('mail.blah.com') popconnection.user('user-name-goes-here') popconnection.pass_('password-goes-here') # Find out how many messages are waiting numMessages = len(popconnection.list()[1]) # Iterate through the messages for i in range(numMessages): # retr will be called three times for each email message in the j loop below. # # The first call will return a string telling you how many bytes (i.e., octets) # are in the message. The string will look like this: OK octets # (where is the total number of bytes in the message). # # The second call will return a list containing the elements of the message. # # The third call will return an integer containing the total number of # bytes in the message for j in popconnection.retr(i+1): # We are interested only in the contents of the list. And we need to put the # list contents into a string, because that is what message_from_string expects. # We must also be sure to put a line break at the end of the substring that # represents each list element, as message_from_string relies on line breaks # to parse the email message. The k loop below builds the string from the list. if type(j) == list: numElements = len(j) outString = "" for k in range(numElements): outString += j[k] outString += '\n' message = email.message_from_string(outString) # Now that we have got the contents of the email into an email object, we can # access the logical elements of the email at will, in any order. The call to # get_payload() is to retrieve the body of the message. print message['Subject'] print message['From'] print message.get_payload() # Strictly speaking, all we need to do on quitting is to disconnect, but in some # applications it would be a good idea to delete the email from the server. popconnection.quit() -- http://mail.python.org/mailman/listinfo/python-list
Python and PHP encryption/decryption
I'm looking for a recommendation about encryption/decryption packages for Python. I'm working on a project that will require me to store some values in a database in encrypted format. I'll be storing them from a PHP script and retrieving them (decrypting them) using Python. I'm currently using PHP's mcrypt package to encrypt the values, and I'm using AES for encryption, so something AES-compatible would be ideal. However, the project is at the development stage so I can easily change how I'm encrypting things in PHP if there is a compelling reason on the Python side of things to do so. Many Thanks, Jean-Claude -- http://mail.python.org/mailman/listinfo/python-list