Regex for extracting filename from path

前端 未结 17 543
时光取名叫无心
时光取名叫无心 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: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.

提交回复
热议问题