Solved

ASP.NET/VB/REGEX:  Make .com Domains Clickable Links

Posted on 2013-11-18
5
194 Views
Last Modified: 2013-11-21
This:
example.com
hello
xyz.example.com
world
abc.xyz.example.com
test
www.experts-exchange.com

Open in new window

should become this:
<a target="_blank" href="http://example.com">example.com</a>
hello
<a target="_blank" href="http://xyz.example.com">xyz.example.com</a>
world
<a target="_blank" href="http://abc.xyz.example.com">abc.xyz.example.com</a>
test
<a target="_blank" href="http://www.experts-exchange.com">www.experts-exchange.com</a>

Open in new window

This only works if it starts with www:
'input = RegularExpressions.Regex.Replace(input, "(?<!\S)(\www\.\S+[^\s@,.""']+)", "<a href=""http://$1"">$1</a>")

Open in new window

0
Comment
Question by:hankknight
  • 3
  • 2
5 Comments
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 39659248
The problem here is that you're removing some insurance to the matches.
Basically you want to match an url that doesn't begin neither by http:// or www

There's no clear safe way of doing this as there are plenty of way to make these matches fail.

I could make one that searches by domain: .com, .net, .whatever...
Basically matching whole lines that end with . something.
But imagine you have an url like:
mydomain.com/test
and a norlam text line like:
I am going there. And there.
They would both match!

So, my final assumption is that:
- the url must not have spaces
- must contain at least one dot before the end

This said try this:
\w+[.-_].+\w+

Although we can still break it...
0
 
LVL 16

Author Comment

by:hankknight
ID: 39659253
This only needs to apply to .com domain names.
0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 39659318
ok, if it's only .com domain names and nothing else after you could use this:
[A-z0-9][\w\._-]+.com

With this you can also have the domains anywhere in the file.
0
 
LVL 16

Author Comment

by:hankknight
ID: 39659456
How can I get that to work with my ASP.NET/VB code like this?
'input = RegularExpressions.Regex.Replace(input, "(?<!\S)(\www\.\S+[^\s@,.""']+)", "<a href=""http://$1"">$1</a>")

Open in new window

0
 
LVL 30

Accepted Solution

by:
Alexandre Simões earned 500 total points
ID: 39659479
Something like this:
Dim strRegex as String = "[A-z0-9][\w\._-]+.com"
Dim myRegexOptions As RegexOptions = RegexOptions.None
Dim myRegex As New Regex(strRegex, myRegexOptions)
Dim strTargetString As String = "!! Your text here !!"
Dim strReplace As String = "<a href=""http://$&"">$1</a>"

Dim result as String = myRegex.Replace(strTargetString, strReplace)

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now