Steven D'Aprano wrote:
On Sat, 14 Mar 2009 01:43:28 pm Lie Ryan wrote:
And Clover wrote:
Here's a simple one I've reinvented in my own apps often enough that it might be worth adding to the built-in split() method:
s.split(sep[, maxsplit[, pad]])
pad, if set True, would pad out the returned list with empty strings (strs/unicodes depending on returned datatype) so that the list was always (maxsplit+1) elements long. This allows one to do things like unpacking assignments:
user, hostname= address.split('@', 1, True)
without having to worry about exceptions when the number of ‘sep’s in the string is unexpectedly fewer than ‘maxsplit’. Can you find a better use case? For splitting email address, I think I would want to know if the address turned out to be invalid (e.g. it does not contain exactly 1 @s)
What makes you think that email address must contain exactly one @ sign?
Email being sent locally may contain zero @ signs, and email being sent externally can contain one or more @ signs. Andy's code:
user, hostname= address.split('@', 1, True)
will fail on syntactically valid email addresses like this:
fred(away @ the pub)@example.com
From Wikipedia: RFC invalid e-mail addresses * Abc.example.com (character @ is missing) * Abc.@example.com (character dot(.) is last in local part) * Abc..123@example.com (character dot(.) is double) * A@b@c@example.com (only one @ is allowed outside quotations marks) * ()[]\;:,<>@example.com (none of the characters before the @ in this example, are allowed outside quotation marks) Your example is valid email address if and only if it is enclosed in quotation mark: "fred(away @ the pub)"@example.com