Solved

ASP.NET/VB/REGEX: Slashes Break Custom Link Markup

Posted on 2013-01-30
7
283 Views
Last Modified: 2013-02-01
I use this ASP.NET / VB / REGEX code:

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

Open in new window


It works with this:
{[link=xyz]}Link Text{[/link]}

Open in new window


But it does not work with this:
{[link=http://www.example.com?a=1&b=2/]}Link Text{[/link]}

Open in new window


The problem is that the slashes mess it up.
0
Comment
Question by:hankknight
  • 4
  • 2
7 Comments
 
LVL 41

Expert Comment

by:guru_sami
ID: 38837579
Can you tell a bit on what you mean by 'does not work' and 'mess it up'?
What would be the resultant output you are expecting and what is it that you are getting?
0
 
LVL 16

Author Comment

by:hankknight
ID: 38840137
With my REGEX,

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

0
 
LVL 41

Assisted Solution

by:guru_sami
guru_sami earned 250 total points
ID: 38840591
I tried this:

string input ="{[link=xyz]}Link Text{[/link]}";
input = Regex.Replace(input, @"\{\[link=([^\]]+)\]\}([^\]]+)\{\[\/link\]\}", @"<a href=""$1"">$2</a>");
Response.Write(input + "<br/>");

string input2 = "{[link=http://www.example.com?a=1&b=2/]}Link Text{[/link]}";
input2 = Regex.Replace(input2, @"\{\[link=([^\]]+)\]\}([^\]]+)\{\[\/link\]\}", @"<a href=""$1"">$2</a>");

Open in new window


I got this output:
<a href="xyz">Link Text</a><br/>
<a href="http://www.example.com?a=1&b=2/">Link Text</a>

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
ID: 38843629
I'm not sure I understand the issue either. The logic appears to work fine for me as well:

Screenshot
Are you certain there isn't some other code influencing the output you are receiving?
0
 
LVL 16

Author Comment

by:hankknight
ID: 38844430
That code gives me an error.  Is it for VB?

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: BC30201: Expression expected.

Open in new window

0
 
LVL 16

Author Comment

by:hankknight
ID: 38844446
kaufmed, you are right.  Other regex is conflicting with it!  Thanks for the tip.  I should have posted all my code. Here it is.  How can the other rules be modified so they do not conflict?  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, "(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
 
LVL 16

Author Comment

by:hankknight
ID: 38844678
I realize that is a separate question so I asked it here.
http://www.experts-exchange.com/Programming/Languages/Regular_Expressions/Q_28017172.html
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
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…

864 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

22 Experts available now in Live!

Get 1:1 Help Now