473,386 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Find & Replace hyperlinks in a string

Hi there,

I have a string containing some hyperlinks. I'd like to replace every
hyperlink with a HTML style link.

Example:
--------
Replace
'http://www.foo.com/any_url'
with
'<a href="http://www.foo.com/any_url">http://www.foo.com/any_url</a>'
What's the best way to do this if I have a few hundret strings to check?

Thanks in advance,
Nico
Nov 27 '07 #1
2 2944
On Nov 27, 1:37 am, Nico Grubert <nicogrub...@gmail.comwrote:
Hi there,

I have a string containing some hyperlinks. I'd like to replace every
hyperlink with a HTML style link.

Example:
--------
Replace
'http://www.foo.com/any_url'
with
'<a href="http://www.foo.com/any_url">http://www.foo.com/any_url</a>'

What's the best way to do this if I have a few hundret strings to check?

Thanks in advance,
Nico
Well, this isn't the most robust and someone will probably say not to
use regular expressions, but the Q&D way is:

import re
fixed = re.sub(r'(http:[^\s\n\r]+)', r'<a href="\1">\1</a>',
your_string)

NB. If the URLs are malformed (like have spaces in them, or are broken
over several lines) this won't work right.

Regards,
Jordan
Nov 27 '07 #2
You mean in Python? 'cause if it is a one time shot kind of thing, I
would simply open the file in my favorite editor (NEdit) and use a
Search and Replace, check the regexp box and type my
expression...something along the lines of ([^:]+)://([^:/]+)(:
([0-9]+))?(/.*) to find URLs and then replace with <a href="\0">\0</
a...if I remember correctly that \0 is the entire match, \1 the
first parenthesised match, etc.

gsal
Nov 27 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Xah Lee | last post by:
suppose you want to do find & replace of string of all files in a directory. here's the code: ©# -*- coding: utf-8 -*- ©# Python © ©import os,sys © ©mydir= '/Users/t/web'
2
by: Mark Rae | last post by:
I've inherited an ASP.NET app and have been asked to fix the following problem with a page which is used to display and/or delete records from a database. When the page loads, it displays a list of...
25
by: Neo Geshel | last post by:
This works: <form> <asp:TextBox id="name" /> <%= name.ClientID %> </form> But this DOES NOT work: <form>
19
by: Neo Geshel | last post by:
This works: <form> <asp:TextBox id="name" /> <%= name.ClientID %> </form> But this DOES NOT work: <form>
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.