Solved

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

Posted on 2004-09-08
8
588 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
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

770 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