Faraz Shah Khan

MCP, MCAD.Net, MCSD.Net, MCTS-Win, MCTS-Web, MCPD-Web

Regex to find URL within text and make them as link

Some time back on the form somebody was looking for some help in searching URL within text and make those URLs as link. Me and that guy tried various regex but the one that worked out I thought to put it on the blog so that it can help me and others later. Regex itself is:

-------- In VB.Net ---------
Dim regx As New Regex("http://([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?", RegexOptions.IgnoreCase)

-------- In C#.Net ---------
Regex regx = new Regex("http://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", RegexOptions.IgnoreCase);

And I used following method to convert the URLs into link within text.

-------- In VB.Net ---------
Protected Function MakeLink(ByVal txt As String) As String
        Dim regx As New Regex("http://([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?", RegexOptions.IgnoreCase)

        Dim mactches As MatchCollection = regx.Matches(txt)

        For Each match As Match In mactches
            txt = txt.Replace(match.Value, "<a href='" & match.Value & "'>" & match.Value & "</a>")
        Next

        Return txt
End Function

------- In C#.Net --------
protected string MakeLink(string txt)
{
    Regex regx = new Regex("http://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&amp;\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", RegexOptions.IgnoreCase);
   
    MatchCollection mactches = regx.Matches(txt);
   
    foreach (Match match in mactches) {
        txt = txt.Replace(match.Value, "<a href='" + match.Value + "'>" + match.Value + "</a>");
    }
   
    return txt;
}

Comments

Sandy said:

What about https?

# August 26, 2008 1:31 PM

farazsk11 said:

@Sandy:

Thanks for pointing it out, yes the above regex will not work for https however you can make a small change in the above regex as bleow and it will work for https as well. What I added in the above regex is (s)? after http.

"http(s)?://([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&amp;\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?"

# August 27, 2008 1:32 AM

iyeru said:

What about PHP? =/

# September 1, 2008 11:03 AM

farazsk11 said:

@iyeru

well dude I have no idea about PHP as I have never worked on it.

# September 2, 2008 12:23 PM

Manish Mishra said:

Hay dude try this regular expression string.

"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?"

# September 25, 2008 7:36 AM

jspass2 said:

om

# October 3, 2008 7:03 AM

imperialx said:

How about on a URLRewrite like <a href="/category/books/ISBN-123456">?

# October 21, 2008 7:44 AM

r-dog said:

try this, it will get rid of any link that starts with http:// or https://

"(http|https)://([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&amp;\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?"

# January 16, 2009 1:01 PM

Dominika said:

How about extractingt the link name associated with that found link as well?

# March 3, 2009 11:30 AM

kotresha said:

Excellent, this saved my day work

# July 14, 2009 7:31 AM

CM said:

Thanx a bunch!

# August 4, 2009 7:59 PM

Confesso que... said:

I use this function here http://www.confessoque.com...works fine! Thanks!

# August 20, 2009 1:12 PM

Baba said:

hi all http://www.google.com/ is a websitr

# September 2, 2009 1:28 AM

Ian Peake said:

Does anyone have a comple piece of code showing how I can read in a text file using streamreader and extract all the lines of text that contain a url? I have tried with RegEx but seem to be getting nowhere. Any help is much appreciated. You can mail me ianpeake@warwickshire.gov.uk

# September 11, 2009 4:30 AM

Kristian Ask said:

Doing a text replace will not work. If there are two links that look the same it will fail to replace. You should use the RegEx.Replace instead.

data = Regex.Replace(

               data,

               @"(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])",

               delegate(Match match)

               {

                   return string.Format("<a href=\"{0}\">{0}</a>", match.ToString());

               });

# October 7, 2009 2:13 AM

Vladimir said:

Very nice example. Thanks for saving me some time!

# October 26, 2009 5:40 AM

C#Beginer said:

Hi,

Can anyone help in Regular Experssion.

I need an expression to extract URL only from <a href tags and ignore other link on the page.

Many thanks,

# November 3, 2009 11:15 AM

Guru said:

Nice regular expressions for finding URLs from plain text.

But if I want to make following url clickable then what would be the regex for the same. e.g. "google.com, yahoo.co.in"

I tried hard to make a regex for the same but not yet succeded.

# March 30, 2010 5:26 AM

Hamidreza said:

Thanks very much....

# April 18, 2010 5:41 PM

Akhilesh said:

ya man its working fine.

Thanks

AKH

# April 28, 2010 8:32 AM

anis said:

thaaank you man :)

# April 30, 2010 11:01 AM

???????????? ????????? http ????????? anchor ?????? ????????? | 2nd Step said:

Pingback from  ???????????? ????????? http ????????? anchor ?????? ????????? | 2nd Step

# July 26, 2010 8:15 PM

Yung said:

C# | Regular expression to find a URL and create anchor tag

# September 13, 2010 7:49 PM

Andreas said:

Google Links like this dosen't work

www.google.se/imgres

# October 12, 2010 7:38 AM

polas said:

Hi,i'm Poll and i need your help i'm making program,which can extract all links from a website or

i need help how to get regex code to extract from text

keyword or just all websites links?

Thanks.

# December 27, 2010 11:21 AM

DE said:

WOW!  Faraz and Kristian you are awesome!  Saved sooo much time.

# December 30, 2010 11:52 PM

Kate Kripke said:

It is extremely interesting for me to read the blog. Thanx for it. I like such topics and everything that is connected to this matter. I would like to read more soon.    

Kate  Kripke  

<a href="indianescortmodels.com/">Indian escorts services</a>

# February 19, 2011 1:33 AM

Jenny Kripke said:

It was certainly interesting for me to read this article. Thank author for it. I like such topics and anything connected to this matter. I definitely want to read a bit more soon.    

Jenny  Kripke    

<a href="www.latinescortlondon.com/">brazilian escorts London</a>

# February 28, 2011 5:10 PM

Jenny Kripke said:

It was extremely interesting for me to read the blog. Thanx for it. I like such topics and anything connected to this matter. I would like to read a bit more soon.      

Jenny  Kripke    

<a href="www.jammer-store.com/">cheap cell phone jammer</a>

# March 5, 2011 10:38 AM

Chavat_Mulga said:

This did wonders for me

Public Shared Function MakeLinks(ByVal sTextToConvert As String) As String

               Return Regex.Replace(sTextToConvert, "((https?:\/\/|www\.)([-\w\.]+)+(:\d+)?(\/([\w\/_\.]*(\?\S+)?)?)?)", "<a href=""$0"" target=""_blank"">$0</a>")

           End Function

# March 10, 2011 10:19 AM

new dumb blonde jokes discount said:

What?, www.gravatar.com/newdumbblondejokess Cheap new dumb blonde jokes,  30501,

# March 13, 2011 8:58 AM

Jeff Albano said:

Thanks much Man!

# March 21, 2011 2:01 AM

David said:

This one <code>((https?|ftp|gopher|telnet|file):((//)|(\\\\))+[\\w\\d:#@%/;$()~_?\\+-=\\\\\\.&]*)</code>

support several protocols and splits the urls in capturing groups.<br/>

More details at

www.java-tutorial.ch/.../extract-urls-using-java-regular-expressions

# June 20, 2011 6:47 AM

polas said:

How to make regex match code for vb.net to extract domain names but only related with http://www.relatedsites.com

Example: http://www.vbforums.com and find related to this forums.

I type vb.net forums and i want to extract it only related vb forums doman names.

How can i do that ?

Thanks.

# August 14, 2011 8:36 AM

Rilevatore links - - MasterDrive.it said:

Pingback from  Rilevatore links -  - MasterDrive.it

# October 31, 2011 2:00 PM

danzig said:

I found this tutorial and could help to make it in automatic mode from a list

www.emanuelz.com.mx/.../extract-links-using-regular-expressions-with-vb-net-128

# November 2, 2011 5:17 AM

Rose23Wilson said:

Cool post  at least I thing so. Keep writing such stuff!

Rose 23Wilson

<a href="dutch-escort.com/">holland escort services</a>

# November 2, 2011 4:22 PM

ravi said:

i want to detect the presence of a textbox in webpage through console...

# November 17, 2011 5:22 AM

PhipsPhep said:

# November 23, 2011 4:00 AM

John said:

What about CLASSIC ASP???

# December 9, 2011 10:17 AM

kaore said:

I wrote a little method that does that in javascript/jquery: tech.cibul.net/turn-urls-into-links-in-text-with-jquery

# December 10, 2011 8:47 AM

pgaljhp said:

# January 18, 2012 8:54 PM

Bespoke Programming said:

Best Article

# February 7, 2012 8:43 AM

Eranda said:

Really useful, Thanks

# February 20, 2012 12:27 AM

Regex to find URL within text and make them as link | Regular Expression Library said:

Pingback from  Regex to find URL within text and make them as link | Regular Expression Library

# March 4, 2012 4:24 AM

sabry said:

Thanks a million you saved me a lot of time

# May 11, 2012 4:17 AM

regex for parsing url from html code | PHP Developer Resource said:

Pingback from  regex for parsing url from html code | PHP Developer Resource

# May 23, 2012 3:07 PM

Ravi said:

Thanks. its really helpful for me. Thanks again

# May 24, 2012 1:23 PM

Mass Forum Poster said:

Thanks, it save my time

# August 6, 2012 5:22 AM

Cousalya, said:

Thanks gud job:)its literally works fine..........

# August 17, 2012 2:46 AM

Carlos said:

Excellent! thanks

# August 30, 2012 5:09 PM

Emang Oke said:

why this is stil not work for me

# September 10, 2012 2:38 AM

Schubert said:

Greate article. Keep writing such kind of information on your

page. Im really impressed by your blog.

Hello there, You have done an excellent job. I'll certainly digg it and personally recommend to my friends. I'm sure they will be benefited from

this website.

# October 27, 2012 2:26 PM

Pettis said:

Hello to every body, it's my first pay a visit of this webpage; this weblog consists of awesome and truly good stuff in favor of visitors.

# December 12, 2012 5:47 PM

Lind said:

I hardly drop remarks, but i did a few searching and

wound up here Regex to find URL within text and make them as link

- Faraz Shah Khan. And I actually do have 2

questions for you if it's allright. Is it simply me or does it look like some of the remarks look like they are written by brain dead folks? :-P And, if you are writing at additional social sites, I'd like to

keep up with anything new you have to post. Would you list of all of all your social pages like your

twitter feed, Facebook page or linkedin profile?

# December 16, 2012 5:30 AM

tmerrey said:

Brilliant, thanks a bunch :)

# April 19, 2013 7:59 AM

Kavimaran said:

Save my day, thanks :-)

# May 7, 2013 6:23 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)