Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Regex needed

Posted on 2004-10-27
12
Medium Priority
?
259 Views
Last Modified: 2010-04-15
I need a little bit of code that can take a string:
"/images/banners/chocolate_time.gif"

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.
0
Comment
Question by:OliWarner
[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
  • 8
  • 3
12 Comments
 
LVL 15

Accepted Solution

by:
Timbo87 earned 2000 total points
ID: 12429270
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("_", " ");
Console.WriteLine(name);

Output:
chocolate time
0
 
LVL 16

Author Comment

by:OliWarner
ID: 12429356
what links the var file to the regex?
is that a type when you say file1 (..."Match(file1).Groups"...)
0
 
LVL 15

Expert Comment

by:Timbo87
ID: 12429376
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.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 16

Author Comment

by:OliWarner
ID: 12429431
yeah it all makes sense.
Is null checking a safe way to see if there was no match?
0
 
LVL 16

Author Comment

by:OliWarner
ID: 12429472
http://www.thepcspy.com/images/banners/Thanks_Timbo.gif

ASP is just a little too powerful for its own good.
Thanks for your help today.
0
 
LVL 16

Author Comment

by:OliWarner
ID: 12429485
befroe you ask its a dynamic imaging thing i've been working on... the actual script is

www.thepcspy.com/images/mat.aspx?title=pie

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.
0
 
LVL 16

Author Comment

by:OliWarner
ID: 12429526
one more thing...

if i try a pass a period through eg: http://www.thepcspy.com/images/banners/THEPCSPY.com.gif
the script runs as a picture is displayed, but no text from the input. commas do the same.
0
 
LVL 16

Author Comment

by:OliWarner
ID: 12429573
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"?
0
 
LVL 15

Expert Comment

by:Timbo87
ID: 12429786
Yes, or you could just add \. to allow periods.
0
 
LVL 3

Expert Comment

by:nitrogenx
ID: 12429852
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.
0
 
LVL 16

Author Comment

by:OliWarner
ID: 12429920
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: www.thepcspy.com/a_random_non-existant_image.gif

if its not an image (jpg/gif/png) it will show a real 404.
0
 
LVL 16

Author Comment

by:OliWarner
ID: 12429928
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 ASP.net and databases...
I'll get around to it one day if I can justify the bandwidth sink.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Loops Section Overview

610 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