Regex for path parsing

This is a discussion on Regex for path parsing within the C# Programming forums, part of the General Programming Boards category; Hi, I want to use regex to parse a path. Say I want to extract test.zip from /Test/test.zip. Could you ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    141

    Regex for path parsing

    Hi,

    I want to use regex to parse a path. Say I want to extract test.zip from /Test/test.zip.
    Could you please let me know how to proceed?

    Angkar

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Quote Originally Posted by AngKar
    I want to use regex to parse a path.
    Okay. Why?
    Quote Originally Posted by AngKar
    Say I want to extract test.zip from /Test/test.zip.
    Could you please let me know how to proceed?
    Use Path.GetFileName(string). Most of the things you need to parse from a path can be done with the static methods on the Path class.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    Just for fun:

    Code:
    [a-zA-Z0-9]+\.{1}[a-zA-Z0-9]{3}$
    should do the trick... that is a file needs to have an extension of 3 characters , and only one extension is allowed.

    If you only want to get zip files you could go with something like:

    Code:
    [a-zA-Z0-9]+\.{1}zip$

    This is all assuming the filename is at the end of the line where you will do the regexp check.

    If you want to play and test reg. expressions get something like regexp coach ( it's pretty cool ).

    But if I was you I'd go with pianorain's advice, its in the framework so use it , instead of writing code that will make people look twice to understand what you want to achieve.

    :edit:
    Meh, seems like tést.zip wouldn't be valid with those reg. expressions ... now I'm sad .

  4. #4
    Registered User Jaqui's Avatar
    Join Date
    Feb 2005
    Posts
    416
    Quote Originally Posted by GanglyLamb
    Meh, seems like tést.zip wouldn't be valid with those reg. expressions ... now I'm sad .
    because the accented e isn't in the characters you specified for the regex. a lot of charsets don't have the accented characters in them, if you match from the last / until the first . then the remainder with wildcards, then any valid character will cause a match.
    Quote Originally Posted by Jeff Henager
    If the average user can put a CD in and boot the system and follow the prompts, he can install and use Linux. If he can't do that simple task, he doesn't need to be around technology.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    Thanks guys...got it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need sth about parsing
    By Masterx in forum C++ Programming
    Replies: 6
    Last Post: 11-07-2008, 12:55 AM
  2. My own regex...............class?
    By misplaced in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2005, 10:18 AM
  3. <regex.h> regex syntax in C
    By battersausage in forum C Programming
    Replies: 7
    Last Post: 03-24-2004, 01:35 PM
  4. Parsing for Dummies
    By MisterWonderful in forum C++ Programming
    Replies: 4
    Last Post: 03-08-2004, 05:31 PM
  5. I hate string parsing with a passion
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 03-19-2002, 07:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21