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
I've tried several things, most based off of something like http://regexr.com?302m5 but can't quite get there
^\\(.+\\)*(.+)\.(.+)$
This regex has been tested on these two examples:
\var\www\www.example.com\index.php
\index.php
First block "(.+\)*" matches directory path.
Second block "(.+)" matches file name without extension.
Third block "(.+)$" matches extension.
This will get the filename but will also get the dot. You might want to truncate the last digit from it in your code.
[\w-]+\.
Update
@Geoman if you have spaces in file name then use the modified pattern below
[ \w-]+\. (space added in brackets)
Demo
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With