Help me to improve this URL replacing RegExp
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngregexp' list.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

Felipe Coury
I have this code in ASP.NET:

Function FetchURL( strMessage As String )

Dim strPattern As String =
"(?<url>http://(?:[\w-]+\.)+[\w-]+(?:/[\w-./?%&~=]*[^.])?)"
Dim strReplace As String = "<a href='${url}' target='_blank'>${url}</a>"
Dim strInput As String = strMessage
Dim strResult As String

strResult = Regex.Replace( strInput, strPattern, strReplace )

strPattern =
"(?<!http://)(?<url>www\.(?:[\w-]+\.)+[\w-]+(?:/[\w-./?%&~=]*[^.])?)"
strResult = Regex.Replace( strResult, strPattern, strReplace )

Return strResult

End Function

The code is brilliant, but it fails in some matcher/replaces:

#1
bla bla (http://www.cade.com.br) bla bla
becomes
bla bla (<a href="http...">http://www.cade.com.br)</a> bla bla
should be
bla bla (<a href="http...">http://www.cade.com.br</a>) bla bla

#2
http://www.aspcode.com/showcode.asp?id=203&secion=Bla
becomes
<a href="...">http://www.aspcode.com/showcode.asp?id=203&</a>section=Bla
should be
<a href="...">http://www.aspcode.com/showcode.asp?id=203§ion=Bla</a>

#3
www.hello.com
becomes
<a href='www.hello.com'>www.hello.com</a>
should be
<a href='http://www.hello.com'>www.hello.com</a>

#4
http://www.code.com<br>
becomes
<a href='...'>http://www.code.com<</a>br>
should be
<a href='...'>http>//www.code.com</a><br>

Thanks in advance,

Felipe Coury

Reply to this message...
 
    
Yannick Smits
I tested the code with my version and it works for al 4 instances! Check this sample page (watch out for wrapping!):

---code---
<%@ Page language="c#" AutoEventWireup="false" %>

<script language="C#" runat=server>
public void Button1_Click (object sender, System.EventArgs e)
{
string strPattern = @"(?<url>http://(?:[\w-]+\.)+[\w-]+(?:/[\w-./?%&~=]*[^.\s|,|\)|!])?)";
string strReplace = "<a href=\"${url}\" target=_blank>${url}</a>";
string strInput = TextBox1.Text;
string strResult;
strResult = Regex.Replace(strInput, strPattern, strReplace);
strPattern = @"(?<!http://)(?<url>www\.(?:[\w-]+\.)+[\w-]+(?:/[\w-./?%&~=]*[^.\s|,|\)|!])?)";
strReplace = "<a href=\"http://${url}\" target=_blank>${url}</a>";
strResult = Regex.Replace(strResult, strPattern, strReplace);

myLabel.Text = strResult;
}
</script>
<html><head><head><body>
<form method="post" runat="server">
<p>
<asp:TextBox id=TextBox1 textmode="Multiline" rows=5 columns=60 runat="server">
bla bla (http://www.cade.com.br) bla bla
http://www.aspcode.com/showcode.asp?id=203&secion=Bla
www.hello.com
http://www.code.com<br>
</asp:TextBox></p>
<p>Result: <asp:Label id=myLabel runat="server" /></p>
<p><asp:Button id=Button1 runat="server" Text="Button" OnClick="Button1_Click"></asp:Button></p>
</form>
</body></html>
---/code---

hth,
Yannick Smits

"Felipe Coury" <Click here to reveal e-mail address> wrote in message news:472610@aspngregexp...
[Original message clipped]

Reply to this message...
 
    
Felipe Coury (VIP)
Yannick,

Thanks for the reply, but try with this text:

"Check out
from<br>http://www.creation.com.br/fcoury/teste.asp?name=felipe&age=22.<
br><br>This should work:<br><br>o www.cade.com<br>o
http://geocities.com<br>o http://pop.creation.com.br<br>o
www.aa.com/name.cgi?report=S<br><br>How do you like it?!<br><br>Felipe
Coury<br>http://www.creation.com.br<br><br></font><br>"

You'll note that:

http://www.creation.com.br/fcoury/teste.asp?name=felipe&age=22.

Still hilights wrong

www.aa.com/name.cgi?report=S<br>

highlites the < on the <br>

and www.cade.com gets linked to www.cade.com and not http://www.cade.com.

The code for FetchURL I'm using is:

Function FetchURL( strMessage As String )

Dim strPattern As String =
"(?<url>http://(?:[\w-]+\.)+[\w-]+(?:/[\w-./?%&~=]*[^.\s|,|\)|!])?)"
Dim strReplace As String = "<a href='${url}' target='_blank'>${url}</a>"
Dim strInput As String = strMessage
Dim strResult As String

strResult = Regex.Replace( strInput, strPattern, strReplace )

strPattern =
"(?<!http://)(?<url>www\.(?:[\w-]+\.)+[\w-]+(?:/[\w-./?%&~=]*[^.\s|,|\)|!])?
)"
strResult = Regex.Replace( strResult, strPattern, strReplace )

Return strResult

End Function

Regards,

Felipe Coury
Best regards.

----- Original Message -----
From: "Yannick Smits" <Click here to reveal e-mail address>
Newsgroups: aspngregexp
To: "aspngregexp" <Click here to reveal e-mail address>
Sent: Friday, August 31, 2001 11:42 AM
Subject: [aspngregexp] Re: Help me to improve this URL replacing RegExp

> I tested the code with my version and it works for al 4 instances! Check
this sample page (watch out for wrapping!):
[Original message clipped]

?)";
[Original message clipped]

Reply to this message...
 
    
Yannick Smits
You're right here for the <br> problem but www.cade.com works fine with my code. The <br> problem is solved by adding |< like this:

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

hth,
Yannick Smits

"Felipe Coury" <Click here to reveal e-mail address> wrote in message news:473023@aspngregexp...
[Original message clipped]

Reply to this message...
 
 
System.EventArgs
System.Text.RegularExpressions.Regex
System.Web.UI.WebControls.TextBox
System.Windows.Forms.TextBox




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com