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?
Timbo87Connect With a Mentor Commented:
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
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.
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

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.
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.

All Courses

From novice to tech pro — start learning today.