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

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?
Who is Participating?
ko_min_minConnect With a Mentor Commented:
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

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!
SmoerbleAuthor Commented:
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.
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

SmoerbleAuthor Commented:
SmoerbleAuthor Commented:
*bump with 500 pts.*
Results :

fsldfj fsldfj   ==>  fsldfj [url=] fsldfj

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

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


Ko Min Min
SmoerbleAuthor Commented:
works PERFECT!

Thanks a LOT!
SmoerbleAuthor Commented:
Oh, and 500pts for you, and a big-fat-grin-on-my-face :)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.