Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…

704 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