Solved

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

Posted on 2013-02-01
4
323 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 50 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 450 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…

914 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

14 Experts available now in Live!

Get 1:1 Help Now