2

I'm looking for a stand-alone python function that will take in a string and return a string with the email addresses converted to links.

Example:

>>>s = 'blah blah blah [email protected] blah blah blah'
>>>link(s)
'blah blah blah <a href="mailto:[email protected]">[email protected]</a> blah blah blah'
2

3 Answers 3

8

Something like this?

import re
import xml.sax.saxutils

def anchor_from_email_address_match(match):
    address = match.group(0)
    return "<a href=%s>%s</a>" % (
        xml.sax.saxutils.quoteattr("mailto:" + address),
        xml.sax.saxutils.escape(address))

def replace_email_addresses_with_anchors(text):
    return re.sub("\w+@(?:\w|\.)+", anchor_from_email_address_match, text)

print replace_email_addresses_with_anchors(
    "An address: [email protected], and another: [email protected]")
Sign up to request clarification or add additional context in comments.

4 Comments

You misread it: the xml module is used only for HTML-quoting. A regular expression is used to find the email addresses. I messed up my string formatting -- fixed.
Uh oh. Yep, I misread it. Works well regardless. +1 for HTML quoting then :D
This doesn't catch "-" characters in domains like yahoo-inc.com. You can add these by modifying replace_email_addresses_with_anchors: return re.sub("\w+@(?:\w|\.|\-)+", anchor_from_email_address_match, text)
Wouldn't work at my work where everybody has a email address of the form [email protected] so I suggest modifying the re to "(?:\w|\.|\-)+@(?:\w|\.|\-)+"
2
>>> def convert_emails(s):
...     words =  [ word if '@' not in word else '<a href="mailto:{0}">{0}</a>'.format(word) for word in s.split(" ") ]
...     return " ".join(words)
... 
>>> s = 'blah blah blah [email protected] blah blah blah'
>>> convert_emails(s)
'blah blah blah <a href="mailto:[email protected]">[email protected]</a> blah blah blah'
>>> 

Not super robust but works for very basic cases.

2 Comments

Note that DTing isn't bothering to HTML-quote here. You should HTML-quote, unless you control the input and know it can't possibly have characters in it that have special meaning. You could search for "XSS" or "code injection" for the reason why, but this famous cartoon says it better.
+1 For it's simplicity. For my use case this would be fine, I'm just converting text from a requirements document, not from potentially malicious users.
0
def link(s):
    return '<a href="mailto:{0}">{0}</a>'.format(s)

2 Comments

This assumes you've matched the email address and passed it in.
Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you’ve made.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.