Solved

Regex needed

Posted on 2004-10-27
255 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
Question by:OliWarner
    12 Comments
     
    LVL 15

    Accepted Solution

    by:
    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
    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
    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
     
    LVL 16

    Author Comment

    by:OliWarner
    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
    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
    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
    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
    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
    Yes, or you could just add \. to allow periods.
    0
     
    LVL 3

    Expert Comment

    by:nitrogenx
    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
    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
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Title # Comments Views Activity
    Need help with a string operation. 2 24
    Request for sample code - C# and ASP.NET 14 44
    asp.net mvc - Time 37 38
    Asp.net mvc C# 2 18
    Article by: Ivo
    Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
    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…
    This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    877 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now