Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Extract directory name from file path - C#

Last post 07-23-2009, 3:25 AM by komandin.alexander. 3 replies.
Sort Posts: Previous Next
  •  06-24-2009, 6:29 AM 54279

    Extract directory name from file path - C#

    Hi, I have following example:

    C:\C4\trunk\Catalog\bvh\dance\dance2.bvh

    and I need to extract last folder name (dance). The variable parts are - last folder name and file name, so text can be

    C:\C4\trunk\Catalog\bvh\sit\sit_female.bvh  (result should be "sit")

    C:\C4\trunk\Catalog\bvh\sit\sit_male.bvh   (result should be "sit")

    C:\C4\trunk\Catalog\bvh\battle\shoot.bvh    (result should be "battle")

    Thanks,

     -Lev 

     

    Filed under:
  •  06-24-2009, 10:28 AM 54281 in reply to 54279

    Re: Extract directory name from file path - C#

    [^\\]+(?=\\[^\\]+$)


  •  06-25-2009, 5:47 AM 54315 in reply to 54281

    Re: Extract directory name from file path - C#

    Thanks! Exactly what I need. 

     

     

  •  07-23-2009, 3:25 AM 55362 in reply to 54279

    Re: Extract directory name from file path - C#

    Text

    C:\C4\trunk\Catalog\bvh\dance\dance2.bvh
    C:\C4\trunk\Catalog\bvh\sit\sit_female.bvh (result should be "sit")
    C:\C4\trunk\Catalog\bvh\sit\sit_male.bvh (result should be "sit")
    C:\C4\trunk\Catalog\bvh\battle\shoot.bvh (result should be "battle")

    Regex

    .*\\([^\\]+)\\[^\\]+$

    Results Group[1]:

    dance
    sit
    sit
    battle

     

    --------

    More examples here:

    http://komandin.org/regex/regex_view.html



View as RSS news feed in XML