pythondumb
pythondumb

Reputation: 1247

Replace email id by HTML Tag to make a hyperlink inside the text

I have a text as follows

For further details, please contact [email protected]

I want to replace the email ID mentioned in the above text by <a href "[email protected]">[email protected]</a> So that the email ID would become a clickable object when the above text would be presented on web page

So far I have tried out following

text = 'For further details, please contact [email protected]'
email_pat = re.findall(r'[\w\.-]+@[\w\.-]+\.\w+',text)
email_str = ' '.join(email_pat) #converts the list to string
text_rep = ext.replace(email_str,'<a href "email_str">email_str</a>')

The above code replace the email string but instead of creating hyperlink it actually does the following

For further details, please contact <a href "email_str">email_str</a>

Is there any way to tackle this?

Edit When I am using the above solution in Flask, on frontend I am getting the desired result (i.e. email ID becomes clickable, urls become clickable). But when I click on this I am being redirected to the localhost:5002 instead of opening the Outlook. localhost:5002 is where my Flask App is being hosted. Even for the url also it is not working. I am using the following code to make the url string clickable.

text = text.replace('url',f'<a href "{url_link}">{url}</a>'

The above code is making the usr string clickable, but upon clicking it is being redirected to localhost:5002 Is there any change I need to make in app.run(host=5002) method?

Upvotes: 1

Views: 796

Answers (2)

Grismar
Grismar

Reputation: 31354

Your actual problem is the line:

text_rep = ext.replace(email_str, '<a href "email_str">email_str</a>')

That does exactly what you say it does, but what you want is this:

text_rep = ext.replace(email_str, f'<a href "{email_str}">{email_str}</a>')

Instead of replacing the mail address with that literal string with email_str in it, this formats the string so that it has the mail address in it. This is assuming you run Python 3, for Python 2 it would be more like:

text_rep = ext.replace(email_str, '<a href "{email_str}">{email_str}</a>'.format(email_str=email_str))

However, note that your regex to match mail addresses makes some assumptions, a better version can be found here Are email addresses allowed to contain non-alphanumeric characters?

Also, your code assumes there will be only one mail address in the source text string, as you're joining results. A better solution might be one which replaces each individual mail addresses with the correct replacement.

import re

input_text = 'For further details, contact [email protected] or the webmaster [email protected]'

output_text = re.sub(
    r'(?sm)(([^<>()[\].,;:\s@"]+(\.[^<>()[\].,;:\s@"]+)*)|(".+"))@(([^<>()[\].,;:\s@"]+\.)+[^<>()[\].,;:\s@"]{2,})',
    r'<a href="\g<0>">\g<0></a>', input_text)

print(output_text)

Note that this needs nothing but a basic re.sub.

Upvotes: 1

Ajax1234
Ajax1234

Reputation: 71461

You can use re.sub with a lambda:

import re
s = 'For further details, please contact [email protected]'
new_s = re.sub('[\w\.]+@[\w\.]+', lambda x:f'<a href="{x.group()}">{x.group()}</a>', s)

Output:

'For further details, please contact <a href="[email protected]">[email protected]</a>'

Upvotes: 4

Related Questions