Solved

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

Posted on 2013-02-01
4
332 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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
List the words in Notepad++ 16 46
WPF and Powershell button hover change text 5 63
VB Catch Statement in Event Viewer. 2 20
VB.NET Parsing UDP Bytes 15 38
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…

751 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