Getting the file part or file name part using regex for

Posted on 2006-06-02
Last Modified: 2008-02-01
I have been trying to be able to use a regex to parse out a filename and extension from a long path string...For Example

C:\My Documents\lng path\my file.csv

I want to get "my file.csv" returned.  So far  I can get the path with...

This one gets the .csv but I need "my file.csv"

I have been going crazy trying to figure it out...
Thanks in advance
Question by:kenadelglass
    LVL 5

    Expert Comment

    Use the following functions:

    Path.GetDirectoryName(your string here)
    Path.GetFileNameWithoutExtension(your string here)

    check out the Path object in MSDN - it'll solve all your needs.
    LVL 5

    Expert Comment

    here is the link.  It is the Path class.
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    You can also use the FileInfo class:

            Dim path As String = "C:\My Documents\lng path\my file.csv"

            Dim fi As New System.IO.FileInfo(path)

            ' Oddly enough..theres no GetFileNameWithoutExtension() in the FileInfo class!
            MsgBox(fi.Name.Substring(0, fi.Name.Length - fi.Extension.Length))


    See here:
    LVL 10

    Accepted Solution

    MageDribble and IdleMind solutions are the most straight forward (for sure) to retrieve the filename. However, if you want to use RegEx here's a few patterns I came up with - I haven't tested too many derivations of filenames

            Dim path As String = System.Text.RegularExpressions.Regex.Match(FullPath, "^[\w\W]+\\").Value
            Dim fileName As String = System.Text.RegularExpressions.Regex.Match(FullPath, "\\[\w\W][^\\]+$").Value
            Dim fileExt As String = System.Text.RegularExpressions.Regex.Match(fileName, "\.[\w\W][^\.]+$").Value

            Debug.Print("Path: {0}", path)
            Debug.Print("FileName: {0}", fileName.Trim("\"))   'Remove the first backslash if it exists
            Debug.Print("Extension: {0}", fileExt.Trim("."))       'Remove the first period if it exists

    One that I tried, that works
    Path: C:\afasdf\asdfasdf\asdfasdfsad\kasf.asdfasdf\
    FileName: afkasjdfasd32423423*(@#4234234).esf.sdfafsdf
    Extension: sdfafsdf

    LVL 1

    Author Comment

    Thanks to everyone...I was really looking for a regex answer, I know how to do it with System.IO.Path but wanted to figure it out with a regex.  One question, when I use
    [^\\]+$ on my path of  C:\My Documents\lng path\my file.csv I get exactly "my file.csv" which is what I was looking for

    I can't figure out why that is. [^\\]+ tells me get the specific match starting at the beginning of the document which uses the escape \ followed by the \ which would mean find the \ and with the + meaning one or more - so wouldn't it return all the slashes from the beginning of the document?  Why then does it get me everything in between?  What am I missing here?
    LVL 10

    Expert Comment


    if you break it down
    \\ ==> means the backslash
    [\w\W] ==> all characters (both alphanumeric and non-alphanumeric)
    [^\\] ==> Exclude matches that have a backslash at any index in the string
    +$ ==> repeat from the end of the string

    so if you tried "\\[\w\W]+$"
    you would get "\My Documents\lng path\my file.csv", so by excluding a match that has a backslash, you get just the filename i.e. "\my file.csv" then u just have to trim the beginning backslash



    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

    Join & Write a Comment

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    734 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

    17 Experts available now in Live!

    Get 1:1 Help Now