Solved

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

Posted on 2004-09-08
8
590 Views
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:

[url=http://.....]
[img=http://....]

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?
0
Comment
Question by:Smoerble
[X]
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
8 Comments
 

Expert Comment

by:mattieo
ID: 12023652
G'day!

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

And this for the replace string:

[URL:$0]

The regex above matches:
http://www.makeithappen.com.au

but does not match:
[URL:http://www.makeithappen.com.au]


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!
M.
http://www.makeithappen.com.au
0
 

Author Comment

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

fsldfj http://www.smoerble.de/ fsldfj   ==>   fsldfj [URL:http://www.smoerble.de/ fsldfj]

fsldfj [URL=http://www.smoerble.de/] fsldfj  ==>  fsldfj [URL=[URL:http://www.smoerble.de/]] fsldfj

[IMG=http://www.smoerble.de/] fsldfj  ==>  sldfj [IMG=[URL:http://www.smoerble.de/]] 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.
0
 

Author Comment

by:Smoerble
ID: 12054321
*bump*
0
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

by:Smoerble
ID: 12114949
*bump with 500 pts.*
0
 
LVL 1

Accepted Solution

by:
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
0
 
LVL 1

Expert Comment

by:ko_min_min
ID: 12229576
Results :

fsldfj http://www.smoerble.de/ fsldfj   ==>  fsldfj [url=http://www.smoerble.de/] fsldfj

fsldfj [URL=http://www.smoerble.de/] fsldfj  ==>  fsldfj [url=http://www.smoerble.de/] fsldfj

sldfj [IMG=http://www.smoerble.de/] fsldfj  ==>  sldfj [IMG=http://www.smoerble.de/] fsldfj

Regards,

Ko Min Min
0
 

Author Comment

by:Smoerble
ID: 12258267
works PERFECT!
*mehappy*
:)))

Thanks a LOT!
0
 

Author Comment

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

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