Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-09-08
8
Medium Priority
?
595 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

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

Accepted Solution

by:
ko_min_min earned 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
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…
Progress

927 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