Reg Ex needed (c#): find URL, but…

Posted on 2004-09-08
Last Modified: 2008-03-10
Hi all,
For a forum I need a regex for the following case:

User enters some text in a textarea field.
We use UBB code for links and images, so ppl can enter the following things:


Only if a URL is entered without [url= or [img= it should be replaced with [url=http://…]

Basicly I need the RegEx, if someone has a simple C# code for the replacement, that would be perfect (will increase to 500 pts then).

I hope I could make clear, what I need.
Any thoughts?
Question by:Smoerble
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
  • 5
  • 2

Expert Comment

ID: 12023652

What you need is this for Regex:
(?<!\[URL:)http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?

And this for the replace string:


The regex above matches:

but does not match:

So, to solve this, go:

using System.Text.RegularExpressions;


Regex regex = new Regex(@"(?<!\[URL:)http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");
string desiredResult = regex.Replace(inputString, @"[URL:$0]");

Hope this helps!

Author Comment

ID: 12042171
Sorry, it's not working as expected:

fsldfj fsldfj   ==>   fsldfj [URL: fsldfj]

fsldfj [URL=] fsldfj  ==>  fsldfj [URL=[URL:]] fsldfj

[IMG=] fsldfj  ==>  sldfj [IMG=[URL:]] fsldfj

Additinially (I did not ask this in front), it would be nice, if the RegEx could handle both Caps and non-caps "URL" etc.

Any ideas, how to fix these "bugs"?
Increased to 350.

Author Comment

ID: 12054321
Technology Partners: 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!


Author Comment

ID: 12114949
*bump with 500 pts.*

Accepted Solution

ko_min_min earned 500 total points
ID: 12229547
Hi Smoerble

How about this?

using System.Text.RegularExpressions;


Regex re = new Regex(@"(\[URL=|\[url=)*((?<!\[img=|\[IMG=)(http|ftp|https)://[\w-]+(\.[\w-]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?)(\])*");
string result = re.Replace(str, "[url=$2]");

Hope this works... :)

Ko Min Min

Expert Comment

ID: 12229576
Results :

fsldfj fsldfj   ==>  fsldfj [url=] fsldfj

fsldfj [URL=] fsldfj  ==>  fsldfj [url=] fsldfj

sldfj [IMG=] fsldfj  ==>  sldfj [IMG=] fsldfj


Ko Min Min

Author Comment

ID: 12258267
works PERFECT!

Thanks a LOT!

Author Comment

ID: 12258274
Oh, and 500pts for you, and a big-fat-grin-on-my-face :)

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
split53 challenge 7 117
"Black Box" Testing of Control System Software 2 93
Bot application - advice 3 60
Where to download python requests library 6 63
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

756 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