Regex for extracting filename from path

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

    Here a solution to extract the file name without the dot of the extension. I begin with the answer from @Hammad Khan and add the dot in the search character. So, dots can be part of the file name:

    [ \w-.]+\.
    

    Then use the regex look ahead(?= ) for a dot, so it will stop the search at the last dot (the dot before the extension), and the dot will not appears in the result:

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

    reorder, it's not necessary but look better:

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

提交回复
热议问题