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

x
?
Solved

ASP.NET/VB/REGEX: First expression messed up by additional expressions

Posted on 2013-02-01
4
Medium Priority
?
347 Views
Last Modified: 2013-02-05
The first regular expression works however the ones after it mess it up.  How can I prevent proceeding regular expressions from messing up the first one?

' BB Links
' {[link=url]}text{[/link]}
input = RegularExpressions.Regex.Replace(input, "\{\[link=([^\]]+)\]\}([^\]]+)\{\[\/link\]\}", "<a href=""$1"">$2</a>")

' Links
input = RegularExpressions.Regex.Replace(input, "(https?://\S+[^\s@,.""']+)", "<a href=""$1"">$1</a>")
input = RegularExpressions.Regex.Replace(input, "(?<!\S)(\www\.\S+[^\s@,.""']+)", "<a href=""http://$1"">$1</a>")

' Email Addresses
input = RegularExpressions.Regex.Replace(input, "\w[\w\.]*\w?@[\w\.]+\w", "<a href=""mailto:$0"">$0</a>")

Open in new window

0
Comment
Question by:hankknight
  • 2
4 Comments
 
LVL 12

Assisted Solution

by:tel2
tel2 earned 200 total points
ID: 38846149
Hi hankknight,

Please explain the way in which the 1st expression is getting messed up, exactly.  An example may help, too.

Thanks.
tel2
0
 
LVL 16

Author Comment

by:hankknight
ID: 38848833
The problem is that this:
{[link=http://www.example.com/]}Link Text{[/link]}

Open in new window

becomes this:
<a href="<a href="http://www.example.com/">Link">http://www.example.com/">Link</a> Text</a>

Open in new window

The regex should not apply to the links that already are inside <a> tags.
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 1800 total points
ID: 38849246
I believe .NET allows flexible length look behinds, so this change to the 2nd replacement may work:

input = RegularExpressions.Regex.Replace(input, "(?<!href\s*=\s['"])(https?://\S+[^\s@,.""']+)", "<a href=""$1"">$1</a>")

Open in new window


You'll probably want to turn on the ignore case option, since href can be in caps.
0
 
LVL 16

Author Comment

by:hankknight
ID: 38856391
Thanks!


' BB Links
' {[link=url]}text{[/link]}
input = RegularExpressions.Regex.Replace(input, "\{\[link=([^\]]+)\]\}([^\]]+)\{\[\/link\]\}", "<a href=""$1"">$2</a>")

' Links
input = RegularExpressions.Regex.Replace(input, "(?<!\S)(https?://\S+[^\s@,.""']+)", "<a href=""$1"">$1</a>")
input = RegularExpressions.Regex.Replace(input, "(?<!\S)(\www\.\S+[^\s@,.""']+)", "<a href=""http://$1"">$1</a>")

' Email Addresses
input = RegularExpressions.Regex.Replace(input, "(?<!\S)\w[\w\.]*\w?@[\w\.]+\w", "<a href=""mailto:$0"">$0</a>")

Open in new window

0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
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…

926 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