|
| 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
|
|
|
| |
|
| |
| |
| 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/
|
|
|
| |
|
| |
|
| |
| 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]
|
|
|
| |
|
|
| |
|
| |
| 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/
|
|
|
| |
|
| |
|
| |
| 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]
|
|
|
| |
|
| |
|
| |
| 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]
|
|
|
| |
|
|
| |
|
|
|
|
|
|
|
|
BootFX
Reliable and powerful .NET application framework. |
|
|
|
|
|
|