Regex needed

I need a little bit of code that can take a string:

It needs to decipher if the string contains the "/images/banners/", end in ".gif" and capture the contents, replacing "_"s for spaces.

so the output of this should be "chocolate time"

it also needs to do some testing to make sure the 2 conditions are true so that if the string isnt that then it can do something else...

Maximum points because I need it fast but I also want to learn. Bonuses for people going the extra mile.
LVL 16
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

using System.Text.RegularExpressions;

string file = "/images/banners/chocolate_time.gif";
Regex fileName = new Regex(@"/images/banners/(?<image>([\w\d_]+)).gif");

string name = fileName.Match(file1).Groups["image"].Value.Replace("_", " ");

chocolate time

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
OliWarnerAuthor Commented:
what links the var file to the regex?
is that a type when you say file1 (..."Match(file1).Groups"...)
The first function call in the third line, fileName.Match(file)

By the way, that's a typo. :) It should be file, not file1 in my code above.

In a nutshell, if the string matches the template, it extracts the image name out of the string and then replaces _ with a space.
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

OliWarnerAuthor Commented:
yeah it all makes sense.
Is null checking a safe way to see if there was no match?
OliWarnerAuthor Commented:

ASP is just a little too powerful for its own good.
Thanks for your help today.
OliWarnerAuthor Commented:
befroe you ask its a dynamic imaging thing i've been working on... the actual script is

but with a bit of love in my custom 404 error page it now rewrites the url to do a server.transfer to the correct script...

Points well earnt.
OliWarnerAuthor Commented:
one more thing...

if i try a pass a period through eg:
the script runs as a picture is displayed, but no text from the input. commas do the same.
OliWarnerAuthor Commented:
fixed it...
added \W onto the regex. now it reads:

string file = strPassThrough;
Regex fileName = new Regex(@"/images/banners/(?<image>([\w\d\W_]+)).gif");
string name = fileName.Match(file).Groups["image"].Value.Replace("_", " ");

if (name == null) {}
else {}

is that all ok and "safe"?
Yes, or you could just add \. to allow periods.
Thats a bit of genius right there OliWarner, your 404 page makes an image no matter what though, it doesn't display a 404 if its not in the banners directory.
OliWarnerAuthor Commented:
yeah it makes my signature (for furms) if its another file outside of that directory...
I've just fixed that...

that signature is made out of a load of dynamic infomation like ip and browser information...
have a look:

if its not an image (jpg/gif/png) it will show a real 404.
OliWarnerAuthor Commented:
And I'm also thinking of making it database powered and opening up that sort of power for anybody that wants it...

but I've had a few problems with and databases...
I'll get around to it one day if I can justify the bandwidth sink.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.