Solved

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

Posted on 2013-11-18
5
193 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
Comment Utility
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
Comment Utility
This only needs to apply to .com domain names.
0
 
LVL 30

Expert Comment

by:Alexandre Simões
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
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…

772 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

10 Experts available now in Live!

Get 1:1 Help Now