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

x
?
Solved

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

Posted on 2013-02-01
4
Medium Priority
?
341 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
[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
  • 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…

722 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