Solved

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

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

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…

762 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

19 Experts available now in Live!

Get 1:1 Help Now