Solved

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

Posted on 2013-11-18
5
201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…

627 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