.NETGURU
convert to hyperlink
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngregexp' list.


Yannick Smits
How would I search a string for URL's (http:// and www.) and than replace the URL's with the HTML hyperlink around it (<a
href=...</a>)?

Any help appreciated,
Yannick Smits

Reply to this message...
 
    
Remas Wojciechowski
Yannick,

Here's a regexp pattern for matching a url (taken from vs.net and
slightly modified):

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

In order to wrap it up with the <a> tag, you need to use the replace
method of the Regexp object:

string strPattern =
@"(?<url>http://(?:[\w-]+\.)+[\w-]+(?:/[\w-./?%&=]*)?)";
string strInput = TextBox1.Text;
string strReplace = "<a href=\"${url}\">${url}</a>";
string strResult;
strResult = Regex.Replace(strInput, strPattern, strReplace);
Label1.Text = strResult;

NOTES:
1/ watch for wrapping
2/ c# version
3/ assumes following elements:
asp:Textbox (name="TextBox1") containing the string to be processed
asp:Label (name="Label1") used to display the result

hth,
Remas
http://www.aspalliance.com/remas/

--- Yannick Smits <Click here to reveal e-mail address> wrote:
[Original message clipped]

=====
Remas Wojciechowski
http://www.aspalliance.com/remas/

__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail - only $35
a year! http://personal.mail.yahoo.com/

Reply to this message...
 
    
Yannick Smits
Wow that looks realy impressive!
Thanks for your resonse.

Unfortunately the code doesn't transform URL's that start just with www.
If the regex finds www. (as most people are used to start a URL) it should append "http://" in the <a href=..>.
So www.myurl.com would give the result <a href=http://www.myurl.com>www.myurl.com</a>

How should I handle this?

Thanks,
Yannick

"Remas Wojciechowski" <Click here to reveal e-mail address> wrote in message news:401370@aspngregexp...
[Original message clipped]

Reply to this message...
 
    
Remas Wojciechowski
Since the not only the search but also the replace pattern needs to be
changed, you'll need another run on the string. Note, that the order is
relevant. Here's the modified version:

string strPattern =
@"(?<url>http://(?:[\w-]+\.)+[\w-]+(?:/[\w-./?%&=]*)?)";
string strReplace = "<a href=\"${url}\">${url}</a>";
string strInput = "hmmm http://www.yahoo.com is a link but
www.aspalliance.com is also a link.";
string strResult;
strResult = Regex.Replace(strInput, strPattern, strReplace);
strPattern =
@"(?<!http://)(?<url>www\.(?:[\w-]+\.)+[\w-]+(?:/[\w-./?%&=]*)?)";
strReplace = "<a href=\"http://${url}\">${url}</a>";
strResult = Regex.Replace(strResult, strPattern, strReplace);
Label1.Text = strResult;

hth,
Remas
http://www.aspalliance.com/remas/

--- Yannick Smits <Click here to reveal e-mail address> wrote:
[Original message clipped]

=====
Remas Wojciechowski
http://www.aspalliance.com/remas/

__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail - only $35
a year! http://personal.mail.yahoo.com/

Reply to this message...
 
    
Yannick Smits
thanks again!
Maybe you could point me to a good regex resource as it looks really powerful!

The only two URL's that are not converted properly yet are:
-www.xyz.com/xyz. --> <a href="http://www.xyz.com/xyz.">.... (the dot should not be in the string)
-www.xyz.com/~xyz --> <a href="http://www.xyz.com">.... (the ~xyz should be part of URL).

Thanks,
Yannick

"Remas Wojciechowski" <Click here to reveal e-mail address> wrote in message news:402083@aspngregexp...
[Original message clipped]

Reply to this message...
 
    
Yannick Smits
Hi,

I found the answers to both of my questions. Here is the result of the new regular expressions (changes underlined). It's part of a function I use:

public string fetchURL (string message)
{
string strPattern = @"(?<url>http://(?:[\w-]+\.)+[\w-]+(?:/[\w-./?%&~=]*[^.])?)";
string strReplace = "<a href=\"${url}\">${url}</a>";
string strInput = message;
string strResult;
strResult = Regex.Replace(strInput, strPattern, strReplace);
strPattern = @"(?<!http://)(?<url>www\.(?:[\w-]+\.)+[\w-]+(?:/[\w-./?%&~=]*[^.])?)";
strReplace = "<a href=\"http://${url}\" target=_blank>${url}</a>";
strResult = Regex.Replace(strResult, strPattern, strReplace);
return strResult;
}

Thanks again for your time,
Yannick Smits

"Yannick Smits" <Click here to reveal e-mail address> wrote in message news:402292@aspngregexp...
[Original message clipped]

Reply to this message...
 
 
System.Text.RegularExpressions.Regex




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification