Help with regular expression

D

dotNet

Hi!

I have this code, to replace www/http/mailto/ftp-links to a real <a
href>-tag.

----
Dim mDelimit As String = Chr(0)

oRegEx = New
Regex("[^<a](\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+)",
RegexOptions.IgnoreCase)
content = oRegEx.Replace(content, "<a href=""mailto" & mDelimit &
":$1"">$1</a>")

oRegEx = New Regex("[^<a](http://|https://|ftp://|mailto:)(\S)(\S+)",
RegexOptions.IgnoreCase)
content = oRegEx.Replace(content, "<a href=""$1$2" & mDelimit &
"$3"">$2" & mDelimit & "$3</a>")

oRegEx = New Regex("[^<a](www\.(\S)(\S+))", RegexOptions.IgnoreCase)
content = oRegEx.Replace(content, "<a href=""http://$1"">$1</a>")

content = Replace(content, mDelimit, "")
----

But it doesnt work very well, any suggestions?

I also want to be able to write my url
description
and parse that to an a-tag aswell.
I have this code:

----
oRegEx = New Regex("\(.*?)\[\/url\]")
content = oRegEx.Replace(content, "<a href=""$1""
target=""_blank"">$2</a>", RegexOptions.IgnoreCase)
----

The problem with the code above is that if I have code like this:

[url=http://test1.com]test1

test2

test3


the link test1 and test2 will be parsed ok, but the link test3 will stay
in plain text. I guess that is because we have a empty row above the text.


Thanks![/url]
 
D

Du Dang

Hi,

If all you want to do is convert a address into a html link then try this.
================================

Dim sample As String = "www.w3.org text http://w3.org w3.org w3.okey
text king.kong.ca"

' http://|https://|ftp://|mailto:
Dim link As String = Regex.Replace(sample,
"(?<link>(http://|https://|ftp://|mailto:)\S+)", "<a
href=""${link}"">${link}</a>")

' www <===== if u only want to get domains that starts
with www then use this
' link = Regex.Replace(link, "(?<link>(?<=(^)|(\s))www\S+)", "<a
href=""http://${link}"">${link}</a>")

' address doesn't start with http://|https://|ftp://|mailto:
link = Regex.Replace(link,
"(?<link>(?<=(^)|(\s))[\S\.]+(\S){2,3}(?=\s|$))", "<a
href=""http://${link}"">${link}</a>")

Console.WriteLine("Sample: {0}", sample)
Console.WriteLine("Link: {0}", link)

================================
NOTE: these regex doesn't validate any of the addresses
ie 30000.com or http://helloworld still consider a valid domain

btw what is the delimiter (mDelimit) do ?

hope that helps,

Du
 
D

Du Dang

oops .. i think i messed up

just use the

' www <===== if u only want to get domains that starts
with www then use this
' link = Regex.Replace(link, "(?<link>(?<=(^)|(\s))www\S+)", "<a
href=""http://${link}"">${link}</a>")

instead of the other one :-(


Regard,

Du

Du Dang said:
Hi,

If all you want to do is convert a address into a html link then try this.
================================

Dim sample As String = "www.w3.org text http://w3.org w3.org w3.okey
text king.kong.ca"

' http://|https://|ftp://|mailto:
Dim link As String = Regex.Replace(sample,
"(?<link>(http://|https://|ftp://|mailto:)\S+)", "<a
href=""${link}"">${link}</a>")

' www <===== if u only want to get domains that starts
with www then use this
' link = Regex.Replace(link, "(?<link>(?<=(^)|(\s))www\S+)", "<a
href=""http://${link}"">${link}</a>")

' address doesn't start with http://|https://|ftp://|mailto:
link = Regex.Replace(link,
"(?<link>(?<=(^)|(\s))[\S\.]+(\S){2,3}(?=\s|$))", "<a
href=""http://${link}"">${link}</a>")

Console.WriteLine("Sample: {0}", sample)
Console.WriteLine("Link: {0}", link)

================================
NOTE: these regex doesn't validate any of the addresses
ie 30000.com or http://helloworld still consider a valid domain

btw what is the delimiter (mDelimit) do ?

hope that helps,

Du

dotNet said:
Hi!

I have this code, to replace www/http/mailto/ftp-links to a real <a
href>-tag.

----
Dim mDelimit As String = Chr(0)

oRegEx = New
Regex("[^<a](\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-
z0-9]+)",
RegexOptions.IgnoreCase)
content = oRegEx.Replace(content, "<a href=""mailto" & mDelimit &
":$1"">$1</a>")

oRegEx = New Regex("[^<a](http://|https://|ftp://|mailto:)(\S)(\S+)",
RegexOptions.IgnoreCase)
content = oRegEx.Replace(content, "<a href=""$1$2" & mDelimit &
"$3"">$2" & mDelimit & "$3</a>")

oRegEx = New Regex("[^<a](www\.(\S)(\S+))", RegexOptions.IgnoreCase)
content = oRegEx.Replace(content, "<a href=""http://$1"">$1</a>")

content = Replace(content, mDelimit, "")
----

But it doesnt work very well, any suggestions?

I also want to be able to write my url
description
and parse that to an a-tag aswell.
I have this code:

----
oRegEx = New Regex("\(.*?)\[\/url\]")
content = oRegEx.Replace(content, "<a href=""$1""
target=""_blank"">$2</a>", RegexOptions.IgnoreCase)
----

The problem with the code above is that if I have code like this:

[url=http://test1.com]test1

test2

test3


the link test1 and test2 will be parsed ok, but the link test3 will stay
in plain text. I guess that is because we have a empty row above the text.


Thanks![/url]

(.*?)\
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top