473,385 Members | 1,518 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Help with regular expression

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=(.*?)\](.*?)\[\/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:

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!
Nov 20 '05 #1
2 1770
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" <do****@brimba.nu> wrote in message
news:u$*************@TK2MSFTNGP11.phx.gbl...
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=(.*?)\](.*?)\[\/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:

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!

Nov 20 '05 #2
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" <vi*******@hotmail.com> wrote in message
news:SY***********@news04.bloor.is.net.cable.roger s.com...
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" <do****@brimba.nu> wrote in message
news:u$*************@TK2MSFTNGP11.phx.gbl...
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=(.*?)\](.*?)\[\/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:

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!


Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Steve | last post by:
Hello, I am writing a script that calls a URL and reads the resulting HTML into a function that strips out everthing and returns ONLY the links, this is so that I can build a link index of various...
5
by: Bradley Plett | last post by:
I'm hopeless at regular expressions (I just don't use them often enough to gain/maintain knowledge), but I need one now and am looking for help. I need to parse through a document to find a URL,...
4
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go...
6
by: JohnSouth | last post by:
Hi I've been using a Regular expression to test for valid email addresses. It looks like: \w+(\w+)*@\w+(\w+)*\.\w+(\w+)* I've now had 2 occassions where it has rejected and email address...
3
by: Joe | last post by:
Hi, I have been using a regular expression that I don’t uite understand to filter the valid email address. My regular expression is as follows: <asp:RegularExpressionValidator...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
3
by: Zach | last post by:
Hello, Please forgive if this is not the most appropriate newsgroup for this question. Unfortunately I didn't find a newsgroup specific to regular expressions. I have the following regular...
6
by: deepak_kamath_n | last post by:
Hello, I am relatively new to the world of regex and require some help in forming a regular expression to achieve the following: I have an input stream similar to: Slot: slot1 Description:...
14
by: Chris | last post by:
I need a pattern that matches a string that has the same number of '(' as ')': findall( compile('...'), '42^((2x+2)sin(x)) + (log(2)/log(5))' ) = Can anybody help me out? Thanks for any help!
3
by: Mr.Steskal | last post by:
Posted: Wed Jul 11, 2007 7:01 am Post subject: Regular Expression Help -------------------------------------------------------------------------------- I need help writing a regular...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.