Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-11-18
5
Medium Priority
?
207 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 2000 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
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…

783 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