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

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

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

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

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

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

ravi said:

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

# November 17, 2011 5:22 AM

John said:

What about CLASSIC ASP???

# December 9, 2011 10:17 AM

Eranda said:

Really useful, Thanks

# February 20, 2012 12:27 AM

sabry said:

Thanks a million you saved me a lot of time

# May 11, 2012 4:17 AM

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

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