Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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

Posted on 2013-01-30
Medium Priority
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.
Question by:hankknight
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
  • 4
  • 2
LVL 41

Expert Comment

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?
LVL 16

Author Comment

ID: 38840137
With my REGEX,

{[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

LVL 41

Assisted Solution

guru_sami earned 1000 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

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!

LVL 75

Accepted Solution

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

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

Author Comment

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

LVL 16

Author Comment

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

LVL 16

Author Comment

ID: 38844678
I realize that is a separate question so I asked it here.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
Suggested Courses

610 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