Regex for extracting filename from path

前端 未结 17 545
时光取名叫无心
时光取名叫无心 2020-12-03 01:02

I need to extract just the filename (no file extension) from the following path....

\\\\my-local-server\\path\\to\\this_file may_contain-any&character.pdf<

相关标签:
17条回答
  • 2020-12-03 01:48

    also one more for file in dir and root

       ^(.*\\)?(.*)(\..*)$
    

    for file in dir

    Full match  0-17    `\path\to\file.ext`
    Group 1.    0-9 `\path\to\`
    Group 2.    9-13    `file`
    Group 3.    13-17   `.ext`
    

    for file in root

    Full match  0-8 `file.ext`
    Group 2.    0-4 `file`
    Group 3.    4-8 `.ext`
    
    0 讨论(0)
  • 2020-12-03 01:48

    TEST ^(.*[\\\/])?(.*?)(\.[^.]*?|)$

    example:

    /^(.*[\\\/])?(.*?)(\.[^.]*?|)$/.exec("C:\\folder1\\folder2\\foo.ext1.ext")
    

    result:

    0: "C:\folder1\folder2\foo.ext1.ext"
    1: "C:\folder1\folder2\"
    2: "foo.ext1"
    3: ".ext"
    

    the $1 capture group is the folder
    the $2 capture group is the name without extension
    the $3 capture group is the extension (only the last)

    works for:

    • C:\folder1\folder2\foo.ext
    • C:\folder1\folder2\foo.ext1.ext
    • C:\folder1\folder2\name-without extension
    • only name
    • name.ext
    • C:\folder1\folder2\foo.ext
    • /folder1/folder2/foo.ext
    • C:\folder1\folder2\foo
    • C:\folder1\folder2\
    • C:\special&chars\folder2\f [oo].ext1.e-x-t
    0 讨论(0)
  • 2020-12-03 01:50

    For most of the cases ( that is some win , unx path , separator , bare file name , dot , file extension ) the following one is enough:

     // grap the dir part (1), the dir sep(2) , the bare file name (3) 
     path.replaceAll("""^(.*)[\\|\/](.*)([.]{1}.*)""","$3")
    
    0 讨论(0)
  • 2020-12-03 01:51

    This is just a slight variation on @hmd's so you don't have to truncate the .

    [ \w-]+?(?=\.)
    

    Demo

    Really, thanks goes to @hmd. I've only slightly improved on it.

    0 讨论(0)
  • 2020-12-03 01:51

    Try this:

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

    It matches everything except back-slash followed by .pdf at the end of the string.

    You can also (and maybe it's even better) take the part you want into the capturing group like that:

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

    But how you refer to this group (the part in parenthesis) depends on the language or regexp flavor you're using. In most cases it'll be smth like $1, or \1, or the library will provide some method for getting capturing group by its number after regexp match.

    0 讨论(0)
提交回复
热议问题