Convert list of urls to hotlinks

Find, replace, find in files, replace in files, regular expressions

Convert list of urls to hotlinks

Postby friskypirate » Sun Feb 15, 2009 12:54 am

is there a macro available that will convert lists of urls like

http://www.abc.com/widgets/1.html
http://www.abc.com/widgets/2.html
http://www.abc.com/widgets/3.html
..
http://www.abc.com/widgets/99.html

to
<a href="http://www.abc.com/widgets/1.html">http://www.abc.com/widgets/1.html</a>
<a href="http://www.abc.com/widgets/2.html">http://www.abc.com/widgets/2.html</a>
<a href="http://www.abc.com/widgets/2.html">http://www.abc.com/widgets/3.html</a>
<a href="http://www.abc.com/widgets/3.html">http://www.abc.com/widgets/4.html</a>
friskypirate
Newbie
 
Posts: 2
Joined: Sun Feb 15, 2009 12:49 am

Re: Convert list of urls to hotlinks

Postby pietzcker » Sun Feb 15, 2009 4:30 am

That's not really a job for a macro, more for a regular expression. Take note of this article that explains the difficulty in solving this problem, though: Detecting URLs in a block of text

In your case, I would recommend a Perl regular expression replace action as follows:

Search string:

\b((https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[A-Z0-9+&@#/%=~_|])

Replace all with

<a href="\1">\1</a>

This will only find URLs that start with http(s)/ftp/file, not "simplified" URLs that start with www; it also won't find URLs that end in parentheses and some other corner cases. This is just a starting point, so if this works for your real-life data, fine. If not, check out the article linked above whether you need a different pattern.
User avatar
pietzcker
Master
Master
 
Posts: 241
Joined: Sun Aug 22, 2004 11:00 pm

Re: Convert list of urls to hotlinks

Postby friskypirate » Sun Feb 15, 2009 11:05 am

Thank you kindly. This is awesome.
friskypirate
Newbie
 
Posts: 2
Joined: Sun Feb 15, 2009 12:49 am


Return to Find/Replace/Regular Expressions