Solved

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

Posted on 2004-09-08
8
582 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
  • 5
  • 2
8 Comments
 

Expert Comment

by:mattieo
Comment Utility
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
Comment Utility
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
Comment Utility
*bump*
0
 

Author Comment

by:Smoerble
Comment Utility
*bump with 500 pts.*
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 1

Accepted Solution

by:
ko_min_min earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
works PERFECT!
*mehappy*
:)))

Thanks a LOT!
0
 

Author Comment

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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Visual xHarbour 1 74
Using YubiKey with REST API application 2 77
countHi challenge 25 84
mapAB Challlenge 35 84
Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

763 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

12 Experts available now in Live!

Get 1:1 Help Now