Solved

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

Posted on 2004-09-08
8
592 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Simple Linear Regression

623 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